Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What the actual meaning `Caused by: java.lang.RuntimeException: view must have a tag`?

Please inform me if knowing whats tag that wanted.

Caused by: java.lang.RuntimeException: view must have a tag

__BaseActivity.java

    @Override
    public void setContentView(int layoutResID) {

        mBinding.contentParent.removeAllViews();
        DataBindingUtil.inflate(LayoutInflater.from(this), layoutResID, mBinding.contentParent, true);
        super.setContentView(mBinding.getRoot());
    }

__ChildActivity.java

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.my_wallet);
}

ERROR logcat

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mydev}: java.lang.RuntimeException: view must have a tag
        at <more...>
     Caused by: java.lang.RuntimeException: view must have a tag
        at android.databinding.DataBinderMapperImpl.getDataBinder(DataBinderMapperImpl.java:121)
like image 628
Et Crc Avatar asked Nov 07 '18 06:11

Et Crc


People also ask

What is RuntimeException in Java?

RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine. RuntimeException and its subclasses are unchecked exceptions.

What is the superclass of RuntimeException?

Class RuntimeException. public class RuntimeException extends Exception RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine. RuntimeException and its subclasses are unchecked exceptions.

What is an invocationtargetexception in Java?

It mainly occurs when we work with the reflection layer and try to invoke a method or constructor that throws an underlying exception itself. The reflection layer wraps the actual exception thrown by the method with the InvocationTargetException. Let’s try to understand it with an example.

What is the purpose of this exception in Java?

This exception is rise explicitly by programmer or by the API developer to indicate that a method has been invoked at the wrong time. Generally, this method is used to indicate a method is called at an illegal or inappropriate time.


3 Answers

This usually happens when attempting to use DataBindingUtil.inflate() to inflate a layout that does not support data binding. In other words, the layout you're attempting to inflate does not have its root element as <layout>.

I have run into this problem when refactoring an Activity to use data binding, and the Activity has multiple layouts. I successfully refactored one of the layouts to include the <layout> element at its root, but I didn't refactor all the other layouts (layouts for other screen densities, languages, modules, etc.).

Check to make sure ALL the possible matching layouts are configured with <layout> as their root element.

See this developer doc Layouts and binding expressions

like image 194
mukwux Avatar answered Oct 18 '22 23:10

mukwux


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"
    }
}
like image 28
Umar Ahmed Avatar answered Oct 18 '22 23:10

Umar Ahmed


This happened to me due to the library's layout file (the one for which the error was flagged) having the same name as another one in the app module. Both used data binding.

like image 5
Rajath Avatar answered Oct 18 '22 22:10

Rajath