3

My class extend the LinearLayout, I use DataBinding to inflate the layout. But the code throws an exception that it is view tag isn't correct on view:null .

this is my code :

public class DietListView extends LinearLayout {
    private LayoutDietListViewBinding mBinding;
    private List<?> mDietList = new LinkedList<>();
    private LayoutInflater mInflater;

    public DietListView(Context context) {
        this(context,null);
    }

    public DietListView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

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

    private void initView(Context context) {
        mInflater = LayoutInflater.from(context);
        mBinding = DataBindingUtil.inflate(mInflater, R.layout.layout_diet_list_view, null, false);
        addView(mBinding.getRoot());

     }
}

The Layout file is:

<?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"
    xmlns:bind="http://schemas.android.com/apk/res-auto">
<data>

</data>
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        .....

    </LinearLayout>
</layout>
3
  • There is a similiar question, with your exact error here. stackoverflow.com/questions/33116688/… Commented May 7, 2016 at 3:05
  • I have seen it. but it does not work. I use the dataBinding in the custom view class,i do not know is it right Commented May 7, 2016 at 5:59
  • I can't reproduce this. Which version of Android gradle plugin are using? I tested with Android Studio 2.1 and the plugin that comes with it. It is also possible that you're inflating a non-binding layout file. Commented May 10, 2016 at 23:44

3 Answers 3

4

There is a bug related to this. When you do data binding during inflation, it is confusing the data binding framework. Try delaying your inflation until after the data binding framework completes to see if it will work. The bug should be fixed in android gradle plugin 2.2 (Android Studio 2.2), but won't be available in the I/O 2016 preview.

https://code.google.com/p/android/issues/detail?id=204890

Sign up to request clarification or add additional context in comments.

2 Comments

I'm facing this issue right now. It works in runtime but not in the preview. Is there anyway to fix this right now?
A year has gone by and I'm facing this issue with Android Studio 3.0.1. In my case I have a custom view using data binding which is used multiple times in a data bound fragment. I guess I'll remove the binding from the view using the old fashioned way of finding-by-id, and continue using binding only on the fragment, at this point.
3

This is probably not the best way to do it (it's more like a workaround), but what I do is adding the isInEditMode() method, inflate my layout and exit immediately, like this:

LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);    
if(isInEditMode()){
            inflater.inflate(R.layout.your_layout, this);
            return;
        }

After this, you can bind your view without losing the preview feature.

Comments

0

Above answer was helpful, currently I'm using it like this and it shows successfully in the preview without errors and it works fine when running:

if (isInEditMode) {
    LayoutInflater.from(context).inflate(R.layout.layout_keyboard, this, true)
} else {
    binding = LayoutKeyboardBinding.inflate(LayoutInflater.from(context), this, true)
}

Please read what isInEditMode is: Android dev docs

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.