如何在 kotlin 中初始化 lateinit 变量?
How to initialize lateinit variable in kotlin?
我想从 Activity 中的数据 class cartDocs 中获取 "id"。
我正在尝试 运行 此代码,但出现如下错误:
kotlin.UninitializedPropertyAccessException: lateinit 属性 购物车尚未初始化
我也试图删除“?”在数据 class 中。但仍然是同样的问题。
我怎么解决这个问题?
class CartViewActivity : AppCompatActivity(), SwipeRefreshLayout.OnRefreshListener {
lateinit var cart: cartDocs
@RequiresApi(Build.VERSION_CODES.N)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_cart_view)
val token = SharedPreference.getTokenInfo(this)
Client.retrofitService.getCart(token).enqueue(object :Callback<CartResponse> {
override fun onResponse(call: Call<CartResponse>, response: Response<CartResponse>) {
swipeRefreshLo.setOnRefreshListener(this@CartViewActivity)
showdata(response.body()?.docs!!)
val itemId = cart.id
if (itemId!=null){
SharedPreference.setCartId(applicationContext,itemId)
}
}
override fun onFailure(call: Call<CartResponse>, t: Throwable) {
}
})
数据class cartDocs
data class cartDocs(
var id:String?=null,
var title:String?=null,
var stock:Int?=null,
var availableAfter:String?=null,
var price:Int?=null,
var point:Int?=null,
var mainImage:String?=null,
var description:MutableList<cartDescription>,
var amount:Int?=null,
var added:String?=null,
var options:MutableList<cartOptions>,
var unitPrice:Int?=null,
var unitPoint:Int?=null,
var totalPrice:Int?=null,
var totalPoint:Int?=null
)
lateinit
专为需要在对象创建后的某个时间初始化变量的情况而设计 - 例如像 dagger
这样的框架。事实上,它允许将 lateinit
变量用作正常的 not null
值(并摆脱不必要的 ?/!!
运算符),但程序员有责任确保该值在使用前已初始化。
在您的情况下,您尝试在初始化之前使用该变量。所以,你得到了例外。您需要的是在某个时候初始化该值。但是,我会考虑设计 - 似乎你需要的是 nullable
类型并且在这里使用 lateinit
是不合理的。它将强制您在使用该值之前检查该值是否不为空。
如果您对 JVM 中的 lateinit
设计感兴趣,您可以查看我对其他问题的旧回答:How to uninitialize lateinit in Kotlin
我想从 Activity 中的数据 class cartDocs 中获取 "id"。 我正在尝试 运行 此代码,但出现如下错误: kotlin.UninitializedPropertyAccessException: lateinit 属性 购物车尚未初始化 我也试图删除“?”在数据 class 中。但仍然是同样的问题。 我怎么解决这个问题?
class CartViewActivity : AppCompatActivity(), SwipeRefreshLayout.OnRefreshListener {
lateinit var cart: cartDocs
@RequiresApi(Build.VERSION_CODES.N)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_cart_view)
val token = SharedPreference.getTokenInfo(this)
Client.retrofitService.getCart(token).enqueue(object :Callback<CartResponse> {
override fun onResponse(call: Call<CartResponse>, response: Response<CartResponse>) {
swipeRefreshLo.setOnRefreshListener(this@CartViewActivity)
showdata(response.body()?.docs!!)
val itemId = cart.id
if (itemId!=null){
SharedPreference.setCartId(applicationContext,itemId)
}
}
override fun onFailure(call: Call<CartResponse>, t: Throwable) {
}
})
数据class cartDocs
data class cartDocs(
var id:String?=null,
var title:String?=null,
var stock:Int?=null,
var availableAfter:String?=null,
var price:Int?=null,
var point:Int?=null,
var mainImage:String?=null,
var description:MutableList<cartDescription>,
var amount:Int?=null,
var added:String?=null,
var options:MutableList<cartOptions>,
var unitPrice:Int?=null,
var unitPoint:Int?=null,
var totalPrice:Int?=null,
var totalPoint:Int?=null
)
lateinit
专为需要在对象创建后的某个时间初始化变量的情况而设计 - 例如像 dagger
这样的框架。事实上,它允许将 lateinit
变量用作正常的 not null
值(并摆脱不必要的 ?/!!
运算符),但程序员有责任确保该值在使用前已初始化。
在您的情况下,您尝试在初始化之前使用该变量。所以,你得到了例外。您需要的是在某个时候初始化该值。但是,我会考虑设计 - 似乎你需要的是 nullable
类型并且在这里使用 lateinit
是不合理的。它将强制您在使用该值之前检查该值是否不为空。
如果您对 JVM 中的 lateinit
设计感兴趣,您可以查看我对其他问题的旧回答:How to uninitialize lateinit in Kotlin