17

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)
Et Crc
  • 233
  • 1
  • 3
  • 7
  • Looks like your databinding library (or class) has a bug. Try to set tag manually in xml and if lib doen't override them - this should solve your problem. – Arthur Nov 07 '18 at 07:43

11 Answers11

22

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

mukwux
  • 1,276
  • 11
  • 12
8

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"
    }
}
Umar Ahmed
  • 163
  • 1
  • 5
  • 8
5

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.

Rajath
  • 11,787
  • 7
  • 48
  • 62
4

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

}
Et Crc
  • 233
  • 1
  • 3
  • 7
  • passing null as parent is not (generally) recommended and may cause a crash. You can do `(ViewGroup)findViewById(android.R.id.content)` instead and pass `true` as well – Chisko Apr 26 '19 at 23:03
1

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

Muhammad Helmi
  • 395
  • 2
  • 10
0

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

Uzair
  • 1,529
  • 14
  • 17
0

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

Alberto M
  • 1,608
  • 1
  • 18
  • 42
0

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

GiridharaSPK
  • 496
  • 7
  • 17
0

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

<layout>
xml components here...
</layout>
Black4Guy
  • 641
  • 11
  • 28
0

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>
Rule
  • 629
  • 1
  • 9
  • 19
0

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

  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/29943646) – George Sun Sep 28 '21 at 18:43