Android DataBinding, view tag isn't correct on view:null

8.6k views Asked by At

I am trying to create a custom view with data binding. Here is the code for custom view:

package com.xxx.myapplication;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.util.AttributeSet;
import android.widget.FrameLayout;

import com.xxx.myapplication.databinding.DataviewBinding;

/**
 * Created by atp on 12/25/2016.
*/
public class DataView extends FrameLayout {
    DataviewBinding binding;

    public DataView(Context context) {
        super(context);
    }

    public DataView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DataView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public DataView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        binding =  DataviewBinding.bind(this);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        Customer customer = new Customer();
        customer.firstName = "Custom view ";
        binding.setDataSource(customer);
    }
}

I created a layout file dataview.xml and used above custom view inside that:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:tools="http://schemas.android.com/tools">
    <data>
        <variable
            name="dataSource"
            type="com.xxx.myapplication.Customer" />
    </data>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <com.xxx.myapplication.DataView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@{dataSource.firstName}" />
        </com.xxx.myapplication.DataView>
    </LinearLayout>
</layout>

Then I included the dataview.xml in my activity layout:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:bind="http://schemas.android.com/apk/res-auto">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin">

        <include layout="@layout/dataview"/>
    </RelativeLayout>
</layout>

But when I run the program I get the following exception:

java.lang.RuntimeException: Unable to start activity ComponentInfo{---}:android.view.InflateException: Binary XML file line #13: view tag isn't correct on view:null
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2464)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:234)
at android.app.ActivityThread.main(ActivityThread.java:5526)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.view.InflateException: Binary XML file line #13: view tag isn't correct on view:null
at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.xxx.myapplication.MainActivity.onCreate(MainActivity.java:16)
at android.app.Activity.performCreate(Activity.java:6285)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524) 
at android.app.ActivityThread.access$900(ActivityThread.java:154) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:234) 
at android.app.ActivityThread.main(ActivityThread.java:5526) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: java.lang.RuntimeException: view tag isn't correct on view:null
at com.xxxx.myapplication.databinding.DataviewBinding.bind(DataviewBinding.java:128)
at com.xxx.myapplication.databinding.DataviewBinding.bind(DataviewBinding.java:124)
at com.xxx.myapplication.DataView.onFinishInflate(DataView.java:35)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:844)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:838)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:971)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:831)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284) 
at  android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
at com.xxx.myapplication.MainActivity.onCreate(MainActivity.java:16) 
at android.app.Activity.performCreate(Activity.java:6285) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524) 
at android.app.ActivityThread.access$900(ActivityThread.java:154) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391)     
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:234) 
at android.app.ActivityThread.main(ActivityThread.java:5526) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
5

There are 5 answers

0
Mina Wissa On

I believe you need to add the following namespace to your dataview.xml: xmlns:app="http://schemas.android.com/apk/res-auto"

so that it becomes like this:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto
xmlns:tools="http://schemas.android.com/tools">
    <data>
        <variable
            name="dataSource"
            type="com.xxx.myapplication.Customer" />
    </data>
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <com.xxx.myapplication.DataView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@{dataSource.firstName}" />
        </com.xxx.myapplication.DataView>
    </LinearLayout>
</layout>
1
George Mount On

There is a bug that has now been fixed in data binding where the binding during inflation causes a problem. If you move the binding until after the inflation step (unfortunately, onFinishInflate() still counts as during inflation), then you will avoid the bug. This should be fixed in Android Studio 2.3.

0
Sk Kim On

One of the errors may be caused by having a library module in your app and you are only using data binding in the library module make sure that the layout files in your app module are not the same as those in library module. in my case, I had fragment_home.xml in-app module(not using data binding) and also had fragment_home.xml in library module(using databing) renaming one solved this.

0
Yaroslav Mytkalyk On

I had the same problem when I had multiple layouts with the same name in different modules, but when inflating I was referencing the one which was without data binding (not the one starting with layout tag).

0
Hossein Shahdoost On

Usually you get this error because you are trying to bind your DataBinding to a view which already has another DataBinding connected to it.

In order to make sure there is nothing already bound to your view try DataBindingUtil.getBinding(view). It will return the current DataBinding of your view, so You can reuse that instead of trying to create a new DataBinding.