AndroidRuntime:致命异常:主要 进程:com.example.kotlincountries,PID:15240 kotlin.UninitializedPropertyAccessException:lateinit 属性绑定尚未初始化 在 com.example.kotlincountries.adapter.CountryAdapter.onCountryClicked(CountryAdapter.kt:69) 在 com.example.kotlincountries.databinding.ItemCountryBindingImpl$OnClickListenerImpl.onClick(ItemCountryBindingImpl.java:173) 在 android.view.View.performClick(View.java:7506) 在 android.view.View.performClickInternal(View.java:7483) 在 android.view.View.-$$Nest$mperformClickInternal(未知来源:0) 在 android.view.View$PerformClick.run(View.java:29334) 在 android.os.Handler.handleCallback(Handler.java:942) 在 android.os.Handler.dispatchMessage(Handler.java:99) 在 android.os.Looper.loopOnce(Looper.java:201) 在 android.os.Looper.loop(Looper.java:288) 在 android.app.ActivityThread.main(ActivityThread.java:7872) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
我的应用程序给出了你上面提到的错误,它说第 69 行有错误,但你已经尝试了所有的解决方案,但无法修复它。您是编程新手并且正在尝试学习,因此您正在向像我们这样有经验的人寻求帮助。 你能帮帮我吗?
我在我的 gradle 文件中启用了两个选项。
buildFeatures {
viewBinding = true
dataBinding = true
}
我检查了 XML 文件中的连接。
<layout
xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="country"
type="com.example.kotlincountries.model.Country" />
<variable
name="listener"
type="com.example.kotlincountries.adapter.CountryClickListener" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:onClick="@{listener::onCountryClicked}"
android:orientation="horizontal">
<TextView
android:id="@+id/countryUuidText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:text="@{String.valueOf(country.uuid)}">
</TextView>
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:padding="3dp"
android:downloadUrl="@{country.imageUrl}">
</ImageView>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_vertical"
android:layout_weight="3">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{country.countryName}"
android:padding="5dp"
android:textSize="18sp"
android:textStyle="bold">
</TextView>
<TextView
android:id="@+id/region"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{country.countryRegion}"
android:textSize="16sp"
android:padding="5dp">
</TextView>
</LinearLayout>
</LinearLayout>
</layout>
我检查了我在适配器类中获取的数据。
class CountryAdapter(val countryList:ArrayList<Country>): RecyclerView.Adapter<CountryAdapter.CountryViewHolder>(),CountryClickListener {
private lateinit var binding : ItemCountryBinding
class CountryViewHolder(var view:ItemCountryBinding) :RecyclerView.ViewHolder(view.root) {
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CountryViewHolder {
val inflater = LayoutInflater.from(parent.context)
val binding = ItemCountryBinding.inflate(inflater, parent, false)
return CountryViewHolder(binding)
}
override fun getItemCount(): Int {
return countryList.size
}
override fun onBindViewHolder(holder: CountryViewHolder, position: Int) {
holder.view.country=countryList[position]
holder.view.listener=this
}
fun updateCountryList(newCountryList:List<Country>){
countryList.clear()
countryList.addAll(newCountryList)
notifyDataSetChanged()
}
override fun onCountryClicked(v: View) {
val uuid=binding.countryUuidText.text.toString().toInt()
val action=FeedFragmentDirections.actionFeedFragmentToCountryFragment(uuid)
Navigation.findNavController(v).navigate(action)
}
}
你能帮帮我吗?即使我检查了我的 XML 文件中的连接,验证了我的适配器类中的数据,我也找不到解决方案。
错误消息很清楚,声明为 binding
的变量没有初始化,并试图在您的适配器类中使用它。
如果您检查适配器代码(如错误中提到的位置),您会发现问题出在 lateinit
所以只需更改 onCreateViewHolder
适配器方法
来自
onCreateViewHolder
到
val binding = ItemCountryBinding.inflate(inflater, parent, false)
我刚刚删除了binding = ItemCountryBinding.inflate(inflater, parent, false)
,因为您已经声明了val