Please inform me if knowing whats tag that wanted.
Caused by: java.lang.RuntimeException: view must have a tag
__BaseActivity.java
I have used inflater without "attachToRoot". This error always occurs with issues related to inflater in adapter.
Another scenario where this error occurs is in a RecyclerView's ViewHolder.
Avoid initialising a binding instance in the ViewHolder's bind method
class BindingAdapter(private val items: List<Any>): RecyclerView.Adapter<BindingHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BindingHolder {}
override fun onBindViewHolder(holder: BindingHolder, position: Int) {
holder.bindItem(items[position])
}
}
class BindingHolder(view: View): RecyclerView.ViewHolder(view) {
fun bindItem(item: Any) {
//Don't do this
val binding = ItemSampleBinding.bind(itemView)
}
}
The databinding instance should be initialised outside the bind method because ViewHolders could be recycled and in the code above we could be trying to create a binding instance from a view that's already bound.
Instead create the binding instance in initialisation block of the ViewHolder (this can be in init{}
block or just after the class declaration as shown below)
class BindingHolder(view: View): RecyclerView.ViewHolder(view) {
val binding = ItemSampleBinding.bind(view)
fun bindItem(item: Any) {
//Rest of ViewHolder logic
//binding.textView.text = "Something nice"
}
}
Usually happens while learning about data binding in android, we usually use a main_activity.xml and then include content_main.xml but we by mistake put the <layout
tag in content file. We need to put this <layout
and <data
tag in parent file that is used in setContentView()