kotlin.UninitializedPropertyAccessException: lateinit property binding has not been initialized

huangapple go评论142阅读模式
英文:

kotlin.UninitializedPropertyAccessException: lateinit property binding has not been initialized

问题

My application is giving the below error that you mentioned above and it says there is an error on line 69, but you have tried all the solutions and could not fix it. You are new to programming and trying to learn, so you are asking for help from experienced people like us. Can you please help me?

错误消息:

AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.kotlincountries, PID: 15240
kotlin.UninitializedPropertyAccessException: lateinit property binding has not been initialized
at com.example.kotlincountries.adapter.CountryAdapter.onCountryClicked(CountryAdapter.kt:69)
at com.example.kotlincountries.databinding.ItemCountryBindingImpl$OnClickListenerImpl.onClick(ItemCountryBindingImpl.java:173)
at android.view.View.performClick(View.java:7506)
at android.view.View.performClickInternal(View.java:7483)
at android.view.View.-$$Nest$mperformClickInternal(Unknown Source:0)
at android.view.View$PerformClick.run(View.java:29334)
at android.os.Handler.handleCallback(Handler.java:942)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7872)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)

I have enabled two options in my gradle file.

buildFeatures {
    viewBinding = true
    dataBinding = true
}

I checked the connections in the XML file.

<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>

I checked the data I fetched in my adapter class.

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)
    }
}

Could you please help me? I couldn't find a solution even though I checked the connections in my XML file, verified the data in my adapter class.

英文:

my application is giving the below error that you mentioned above and it says there is an error on line 69, but you have tried all the solutions and could not fix it. You are new to programming and trying to learn, so you are asking for help from experienced people like us.
Can you please help me?

Error Message:

> AndroidRuntime: FATAL EXCEPTION: main
> Process: com.example.kotlincountries, PID: 15240
> kotlin.UninitializedPropertyAccessException: lateinit property binding has not been initialized
> at com.example.kotlincountries.adapter.CountryAdapter.onCountryClicked(CountryAdapter.kt:69)
> at com.example.kotlincountries.databinding.ItemCountryBindingImpl$OnClickListenerImpl.onClick(ItemCountryBindingImpl.java:173)
> at android.view.View.performClick(View.java:7506)
> at android.view.View.performClickInternal(View.java:7483)
> at android.view.View.-$$Nest$mperformClickInternal(Unknown Source:0)
> at android.view.View$PerformClick.run(View.java:29334)
> at android.os.Handler.handleCallback(Handler.java:942)
> at android.os.Handler.dispatchMessage(Handler.java:99)
> at android.os.Looper.loopOnce(Looper.java:201)
> at android.os.Looper.loop(Looper.java:288)
> at android.app.ActivityThread.main(ActivityThread.java:7872)
> at java.lang.reflect.Method.invoke(Native Method)
> at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)

I have enabled two options in my gradle file.

 buildFeatures {
        viewBinding = true
        dataBinding = true

    }

I checked the connections in the XML file.

&lt;layout
    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;

    &lt;data&gt;
        &lt;variable
            name=&quot;country&quot;
            type=&quot;com.example.kotlincountries.model.Country&quot; /&gt;
        &lt;variable
            name=&quot;listener&quot;
            type=&quot;com.example.kotlincountries.adapter.CountryClickListener&quot; /&gt;
    &lt;/data&gt;

    &lt;LinearLayout
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;150dp&quot;
        android:onClick=&quot;@{listener::onCountryClicked}&quot;
        android:orientation=&quot;horizontal&quot;&gt;
        &lt;TextView
            android:id=&quot;@+id/countryUuidText&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:visibility=&quot;gone&quot;
            android:text=&quot;@{String.valueOf(country.uuid)}&quot;&gt;

        &lt;/TextView&gt;

        &lt;ImageView
            android:id=&quot;@+id/imageView&quot;
            android:layout_width=&quot;0dp&quot;
            android:layout_height=&quot;match_parent&quot;
            android:layout_weight=&quot;2&quot;
            android:padding=&quot;3dp&quot;
            android:downloadUrl=&quot;@{country.imageUrl}&quot;&gt;
            &lt;/ImageView&gt;
        &lt;LinearLayout
            android:layout_width=&quot;0dp&quot;
            android:layout_height=&quot;match_parent&quot;
            android:orientation=&quot;vertical&quot;
            android:gravity=&quot;center_vertical&quot;
            android:layout_weight=&quot;3&quot;&gt;
            
            &lt;TextView
                android:id=&quot;@+id/name&quot;
                android:layout_width=&quot;wrap_content&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:text=&quot;@{country.countryName}&quot;
                android:padding=&quot;5dp&quot;
                android:textSize=&quot;18sp&quot;
                android:textStyle=&quot;bold&quot;&gt;

            &lt;/TextView&gt;

            &lt;TextView
                android:id=&quot;@+id/region&quot;
                android:layout_width=&quot;wrap_content&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:text=&quot;@{country.countryRegion}&quot;
                android:textSize=&quot;16sp&quot;
                android:padding=&quot;5dp&quot;&gt;


            &lt;/TextView&gt;

        &lt;/LinearLayout&gt;

    &lt;/LinearLayout&gt;
&lt;/layout&gt;

I checked the data I fetched in my adapter class.

class CountryAdapter(val countryList:ArrayList&lt;Country&gt;): RecyclerView.Adapter&lt;CountryAdapter.CountryViewHolder&gt;(),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&lt;Country&gt;){
       
        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)
    }


}

Could you please help me? I couldn't find a solution even though I checked the connections in my XML file, verified the data in my adapter class.

答案1

得分: 0

错误消息明确表示binding变量被声明为lateinit但未初始化,然后在适配器类中尝试使用它。

如果您检查一下适配器的代码(如错误中提到的位置),您会发现问题在于onCreateViewHolder方法。

所以只需在适配器的onCreateViewHolder方法中进行如下更改:

val binding = ItemCountryBinding.inflate(inflater, parent, false)

改为

binding = ItemCountryBinding.inflate(inflater, parent, false)

我只是去掉了val,因为您已经声明了binding

英文:

Error message is clear that binding variable which is declared as lateinit is not initialised and tried to use it in your adapter class .

and if you will check your code of adapter (as location mentioned in error) you will get that problem is in onCreateViewHolder

So just change in onCreateViewHolder method of adapter

From

val binding = ItemCountryBinding.inflate(inflater, parent, false)

To

binding = ItemCountryBinding.inflate(inflater, parent, false)

i have just removed val as you have already declared binding

huangapple
  • 本文由 发表于 2023年2月18日 14:53:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75491696.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定