Reputation: 125
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>
Upvotes: 3
Views: 3635
Reputation: 1
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
Upvotes: 0
Reputation: 814
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.
Upvotes: 3
Reputation: 20926
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
Upvotes: 4