当我从 Spinner 获取值时,应用程序崩溃并显示 kotlin.UninitializedPropertyAccessException
App crashes with kotlin.UninitializedPropertyAccessException when I get value from Spinner
当我对需要更新的值进行硬编码时,我的应用程序可以正常运行并更新 firestore
中的字段。但是,当我使用以下代码从 spinner
获取值以更新 firestore 中的字段时,应用程序崩溃了。
我正在使用 kotlin
和 viewBinding
。
以下是Logcat
中的错误
--------- beginning of crash 2021-06-07 14:00:12.155 23175-23175/com.trad E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.trad, PID: 23175
kotlin.UninitializedPropertyAccessException: lateinit property binding has not been initialized
at com.trad.ui.adapters.OrderStatusListAdapter.access$getBinding$p(OrderStatusListAdapter.kt:32)
at com.trad.ui.adapters.OrderStatusListAdapter$onBindViewHolder.onClick(OrderStatusListAdapter.kt:84)
at android.view.View.performClick(View.java:8160)
at android.widget.TextView.performClick(TextView.java:16222)
at android.view.View.performClickInternal(View.java:8137)
at android.view.View.access00(View.java:888)
at android.view.View$PerformClick.run(View.java:30236)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8512)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
2021-06-07 14:00:12.234 23175-23175/com.trad I/Process: Sending
signal. PID: 23175 SIG: 9
以下是OrderStatusListAdapter.kt
class
指向第 32 行和第 84 行的错误在代码中作为注释提及。
open class OrderStatusListAdapter( //LINE # 32 MENTIONED IN THE ERROR
private val context: Context,
private var list: ArrayList<OrderStatus>,
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
lateinit var newOrderStatus: Spinner
private lateinit var binding: OrderStatusLayoutBinding
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return MyViewHolder(
LayoutInflater.from(context).inflate(
R.layout.order_status_layout,
parent,
false
)
)
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val model = list[position]
if (holder is MyViewHolder) {
GlideLoader(context).loadProductPicture(
model.image,
holder.itemView.iv_order_status_item_image
)
val dateFormat = "dd MMM yyyy HH:mm"
val formatter = SimpleDateFormat(dateFormat, Locale.getDefault())
val calendar: Calendar = Calendar.getInstance()
calendar.timeInMillis = model.order_datetime
val orderDateTime = formatter.format(calendar.time)
holder.itemView.tv_order_status_order_date.text = orderDateTime
holder.itemView.tv_order_status_item_name.text = model.items[0].title
holder.itemView.tv_order_status_item_price.text = "$${model.total_amount}"
model.order_datetime.toString()
holder.itemView.tv_order_status.text = model.order_status
holder.itemView.tv_order_status_order_id.text = model.id
holder.itemView.btn_order_status_change_status.setOnClickListener {
binding.spnOrderChangeStatus.onItemSelectedListener = // LINE 84 MENTIONED IN THE ERROR
object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(
parent: AdapterView<*>?,
view: View?,
position: Int,
id: Long
) {
Toast.makeText(
context,
"Selected value is ${
parent?.getItemAtPosition(position).toString()
}",
Toast.LENGTH_SHORT
).show()
val newstat = parent?.getItemAtPosition(position).toString()
FirestoreClass().updateOrderStatus(model.id, newstat)
}
override fun onNothingSelected(parent: AdapterView<*>?) {
}
}
}
holder.itemView.ib_order_status_delete_product.visibility = View.GONE
holder.itemView.setOnClickListener {
val intent = Intent(context, SoldProductDetailsActivity::class.java)
intent.putExtra(Constants.EXTRA_SOLD_PRODUCT_DETAILS, model)
context.startActivity(intent)
}
}
}
override fun getItemCount(): Int {
return list.size
}
class MyViewHolder(view: View) : RecyclerView.ViewHolder(view)
}
发生崩溃,因为绑定未初始化。更改 ViewHolder 构造函数:
class MyViewHolder(private val binding : OrderStatusLayoutBinding) : RecyclerView.ViewHolder(binding.root)
同时更改 onCreateViewHolder 方法
return ViewHolder(OrderStatusLayoutBinding.inflate(LayoutInflater.from(parent.context), parent,false))
kotlin.UninitializedPropertyAccessException: lateinit property binding has not been initialized
表示您在 binding
属性 初始化之前尝试使用它。
binding
应该在定义布局的地方声明;这通常在 onCreateViewHolder()
回调中,您在该回调中定义了默认充气器而不使用数据绑定,以解决此问题:
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val inflater = LayoutInflater.from(parent.context)
// initialize the binding either use:
binding = DataBindingUtil.inflate(inflater, R.layout.order_status_layout, parent, false)
// or use:
// binding = OrderStatusLayoutBinding.inflate(inflater)
return MyViewHolder(binding)
}
第二个问题:您在 MyViewHolder
中接受 View
作为构造函数参数,而它应该接受 OrderStatusLayoutBinding
的项绑定类型
class MyViewHolder(view: View) : RecyclerView.ViewHolder(view)
解决这个问题:
class MyViewHolder(binding: OrderStatusLayoutBinding) : RecyclerView.ViewHolder(binding.root)
更新
I got the error Unresolved reference: DataBindingUtil alsoDataBindingUtil and inflater in binding = DataBindingUtil.inflate(inflater, R.layout.order_status_layout, parent, false) are red.
您需要在 build.gradle(模块级别)中启用数据绑定,这需要有 kotlin-kapt
插件:
plugins {
…
id 'kotlin-kapt'
}
android {
…
buildFeatures {
dataBinding true
}
}
当我对需要更新的值进行硬编码时,我的应用程序可以正常运行并更新 firestore
中的字段。但是,当我使用以下代码从 spinner
获取值以更新 firestore 中的字段时,应用程序崩溃了。
我正在使用 kotlin
和 viewBinding
。
以下是Logcat
--------- beginning of crash 2021-06-07 14:00:12.155 23175-23175/com.trad E/AndroidRuntime: FATAL EXCEPTION: main Process: com.trad, PID: 23175 kotlin.UninitializedPropertyAccessException: lateinit property binding has not been initialized at com.trad.ui.adapters.OrderStatusListAdapter.access$getBinding$p(OrderStatusListAdapter.kt:32) at com.trad.ui.adapters.OrderStatusListAdapter$onBindViewHolder.onClick(OrderStatusListAdapter.kt:84) at android.view.View.performClick(View.java:8160) at android.widget.TextView.performClick(TextView.java:16222) at android.view.View.performClickInternal(View.java:8137) at android.view.View.access00(View.java:888) at android.view.View$PerformClick.run(View.java:30236) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:246) at android.app.ActivityThread.main(ActivityThread.java:8512) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
2021-06-07 14:00:12.234 23175-23175/com.trad I/Process: Sending signal. PID: 23175 SIG: 9
以下是OrderStatusListAdapter.kt
class
指向第 32 行和第 84 行的错误在代码中作为注释提及。
open class OrderStatusListAdapter( //LINE # 32 MENTIONED IN THE ERROR
private val context: Context,
private var list: ArrayList<OrderStatus>,
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
lateinit var newOrderStatus: Spinner
private lateinit var binding: OrderStatusLayoutBinding
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return MyViewHolder(
LayoutInflater.from(context).inflate(
R.layout.order_status_layout,
parent,
false
)
)
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val model = list[position]
if (holder is MyViewHolder) {
GlideLoader(context).loadProductPicture(
model.image,
holder.itemView.iv_order_status_item_image
)
val dateFormat = "dd MMM yyyy HH:mm"
val formatter = SimpleDateFormat(dateFormat, Locale.getDefault())
val calendar: Calendar = Calendar.getInstance()
calendar.timeInMillis = model.order_datetime
val orderDateTime = formatter.format(calendar.time)
holder.itemView.tv_order_status_order_date.text = orderDateTime
holder.itemView.tv_order_status_item_name.text = model.items[0].title
holder.itemView.tv_order_status_item_price.text = "$${model.total_amount}"
model.order_datetime.toString()
holder.itemView.tv_order_status.text = model.order_status
holder.itemView.tv_order_status_order_id.text = model.id
holder.itemView.btn_order_status_change_status.setOnClickListener {
binding.spnOrderChangeStatus.onItemSelectedListener = // LINE 84 MENTIONED IN THE ERROR
object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(
parent: AdapterView<*>?,
view: View?,
position: Int,
id: Long
) {
Toast.makeText(
context,
"Selected value is ${
parent?.getItemAtPosition(position).toString()
}",
Toast.LENGTH_SHORT
).show()
val newstat = parent?.getItemAtPosition(position).toString()
FirestoreClass().updateOrderStatus(model.id, newstat)
}
override fun onNothingSelected(parent: AdapterView<*>?) {
}
}
}
holder.itemView.ib_order_status_delete_product.visibility = View.GONE
holder.itemView.setOnClickListener {
val intent = Intent(context, SoldProductDetailsActivity::class.java)
intent.putExtra(Constants.EXTRA_SOLD_PRODUCT_DETAILS, model)
context.startActivity(intent)
}
}
}
override fun getItemCount(): Int {
return list.size
}
class MyViewHolder(view: View) : RecyclerView.ViewHolder(view)
}
发生崩溃,因为绑定未初始化。更改 ViewHolder 构造函数:
class MyViewHolder(private val binding : OrderStatusLayoutBinding) : RecyclerView.ViewHolder(binding.root)
同时更改 onCreateViewHolder 方法
return ViewHolder(OrderStatusLayoutBinding.inflate(LayoutInflater.from(parent.context), parent,false))
kotlin.UninitializedPropertyAccessException: lateinit property binding has not been initialized
表示您在 binding
属性 初始化之前尝试使用它。
binding
应该在定义布局的地方声明;这通常在 onCreateViewHolder()
回调中,您在该回调中定义了默认充气器而不使用数据绑定,以解决此问题:
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val inflater = LayoutInflater.from(parent.context)
// initialize the binding either use:
binding = DataBindingUtil.inflate(inflater, R.layout.order_status_layout, parent, false)
// or use:
// binding = OrderStatusLayoutBinding.inflate(inflater)
return MyViewHolder(binding)
}
第二个问题:您在 MyViewHolder
中接受 View
作为构造函数参数,而它应该接受 OrderStatusLayoutBinding
class MyViewHolder(view: View) : RecyclerView.ViewHolder(view)
解决这个问题:
class MyViewHolder(binding: OrderStatusLayoutBinding) : RecyclerView.ViewHolder(binding.root)
更新
I got the
error Unresolved reference: DataBindingUtil alsoDataBindingUtil and inflater in binding = DataBindingUtil.inflate(inflater, R.layout.order_status_layout, parent, false) are red.
您需要在 build.gradle(模块级别)中启用数据绑定,这需要有 kotlin-kapt
插件:
plugins {
…
id 'kotlin-kapt'
}
android {
…
buildFeatures {
dataBinding true
}
}