java.lang.RuntimeException: view must have a tag

问题描述 投票:0回答:0

我已经查看了论坛中所有与此问题相关的主题,但由于我是新手,所以我尝试的解决方法很遗憾没有奏效。如果我分享我的代码块,你能帮我找出我的错误在哪里吗?

<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/countryUuidTxt"
        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:downloadUrl="@{country.imageUrl}"
        android:padding="5dp">
        
    </ImageView>
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_weight="3"
        android:gravity="center">
        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{country.countryName}"
            android:textSize="20sp"
            android:textStyle="bold"
            android:padding="5dp"
            android:layout_marginBottom="5dp"/>
        <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"/>
    </LinearLayout>

</LinearLayout>
</layout>

我的适配器代码块

class CountryAdapter(val countryList: ArrayList<Country>) :
    RecyclerView.Adapter<CountryAdapter.CountryViewHolder>(),CountryClickListener{

    private lateinit var binding : ItemCountryBinding

        class CountryViewHolder(var binding: ItemCountryBinding) : RecyclerView.ViewHolder(binding.root) {


        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CountryViewHolder {
            val inflater = LayoutInflater.from(parent.context)
            val view = DataBindingUtil.inflate<ItemCountryBinding>(inflater, R.layout.item_country, parent, false)
            return CountryViewHolder(view)
        }

    override fun getItemCount(): Int {
        return countryList.size
    }

    override fun onBindViewHolder(holder: CountryViewHolder, position: Int) {

        holder.binding.country=countryList[position]
        holder.binding.listener=this


    }
    fun updateCountryList(newCountryList:List<Country>){
        countryList.clear()
        countryList.addAll(newCountryList)
        notifyDataSetChanged()
    }

    override fun onCountryClicked(v: View) {
        val uuid =ItemCountryBinding.bind(v).countryUuidTxt.text.toString().toInt()
        val action =FeedFragmentDirections.actionFeedFragmentToCountryFragment(uuid)
        Navigation.findNavController(v).navigate(action)
    }
 }

我的点击界面代码块

interface CountryClickListener {
    fun onCountryClicked(v: View)

}

我尝试过无效/缓存、重建等选项

binding tags
© www.soinside.com 2019 - 2024. All rights reserved.