Android 安卓数据绑定&x2014—;视图标记为';视图上不正确:null

Android 安卓数据绑定&x2014—;视图标记为';视图上不正确:null,android,data-binding,Android,Data Binding,我的类扩展了线性布局,我使用数据绑定来膨胀布局。但是代码抛出一个异常,即视图标记在视图上不正确:null 这是我的代码: public class DietListView extends LinearLayout { private LayoutDietListViewBinding mBinding; private List<?> mDietList = new LinkedList<>(); private LayoutInflater m

我的类扩展了
线性布局
,我使用
数据绑定
来膨胀布局。但是代码抛出一个异常,即视图标记在视图上不正确:null

这是我的代码:

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

     }
}
公共类DietListView扩展了LinearLayout{
私有布局DietListViewBinding;
私有列表MDiteList=新建LinkedList();
私人停车场;
公共DietListView(上下文){
这个(上下文,空);
}
公共DietListView(上下文、属性集属性){
这(上下文,属性,0);
}
公共DietListView(上下文、属性集属性、int-defStyleAttr){
super(上下文、attrs、defStyleAttr);
initView(上下文);
}
私有void initView(上下文){
mInflater=LayoutInflater.from(上下文);
mBinding=DataBindingUtil.inflate(mInflater,R.layout.layout\u diet\u list\u view,null,false);
addView(mBinding.getRoot());
}
}
布局文件为:

<?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>

.....

有一个与此相关的错误。在通货膨胀期间进行数据绑定时,会混淆数据绑定框架。尝试将膨胀延迟到数据绑定框架完成之后,看看它是否能工作。该漏洞应该在android gradle插件2.2(android Studio 2.2)中修复,但不会在I/O 2016预览版中提供


这可能不是最好的方法(更像是一种解决方法),但我要做的是添加isInEditMode()方法,膨胀布局并立即退出,如下所示:

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

在此之后,您可以绑定视图,而不会丢失预览功能。

以上回答很有帮助,目前我正在这样使用它,它在预览中成功显示,没有错误,运行时效果良好:

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

请阅读
isInEditMode
是什么:

有一个类似的问题,这里有您的确切错误。我看到了。但它不起作用。我在自定义视图类中使用了数据绑定,我不知道它是否正确,我无法复制它。您使用的是哪个版本的Android gradle插件?我使用Android Studio 2.1及其附带的插件进行了测试。也有可能您正在膨胀一个非绑定布局文件。我现在面临这个问题。它在运行时工作,但不在预览中。现在有没有办法解决这个问题?一年过去了,我在安卓Studio 3.0.1中面临这个问题。在我的例子中,我有一个使用数据绑定的自定义视图,它在数据绑定片段中被多次使用。我想我将使用老式的按id查找的方式从视图中删除绑定,并继续只在片段上使用绑定,现在。