Android Kotlin 开发问题:‘lateinit‘ modifier is not allowed on properties of primitive types.
📅 2026/7/16 14:03:49
👁️ 阅读次数
📝 编程学习
privatelateinitvaruserId:Int- 在 Android Kotlin 开发中,上述代码出现如下错误信息
'lateinit' modifier is not allowed on properties of primitive types.问题原因
lateinit 不能用于基本类型,Int 是 Kotlin 的基本类型
lateinit 只适用于引用类型
处理策略
- 使用默认值
privatevaruserId:Int=0- 或者,使用可空类型,但需要安全调用
privatevaruserId:Int?=null...userId?.let{...}
编程学习
技术分享
实战经验