Et Crc
Et Crc

Reputation: 233

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)

Upvotes: 17

Views: 13049

Answers (11)

Muhammad Helmi
Muhammad Helmi

Reputation: 435

I have removed dataBinding <layout></layout> TAG and it works for me.

Upvotes: 3

peparonieee
peparonieee

Reputation: 1

I had the same error, I have simply clean my project :) and it's Work

Upvotes: 0

Rule
Rule

Reputation: 630

I converted my layout by wrapping it in <layout> tags, but I forgot to move the namespace declarations to the opening <layout> tag:

Before

<layout>
    <androidx.constraintlayout.widget.ConstraintLayout 
     xmlns:android="http://schemas.android.com/apk/res/android">

After

<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 0

Black4Guy
Black4Guy

Reputation: 649

Please convert the XML layout into data binding. this may cause this exception.like

<layout>
xml components here...
</layout>

Upvotes: 0

GiridharaSPK
GiridharaSPK

Reputation: 547

I have used inflater without "attachToRoot". This error always occurs with issues related to inflater in adapter.

Upvotes: 0

Umar Ahmed
Umar Ahmed

Reputation: 191

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

Upvotes: 9

Rajath
Rajath

Reputation: 11946

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.

Upvotes: 5

Alberto M
Alberto M

Reputation: 1760

this happened to me because one layout wasn't present for all resolution (it was only in layout and layout-normal).

Upvotes: 0

Uzair
Uzair

Reputation: 1538

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

Upvotes: 0

mukwux
mukwux

Reputation: 1316

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

Upvotes: 22

Et Crc
Et Crc

Reputation: 233

NO idea, but working. 😊

  @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.my_wallet, null, false);
            setContentView(mBinding.getRoot());
    }

OR

if your root layout must match as match_parrent for hight/width. like https://github.com/umano/AndroidSlidingUpPanel

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout._activity_layout, null, false);

        // TODO resolve this concurrent assignment
        // tricky method because sliding layout must be as parent / high is HIGH_EXACT to MATCH_PARENT 
        setContentView(mBinding.getRoot(), new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

}

Upvotes: 4

Related Questions