【问题标题】:Android DataBinding —— view tag isn't correct on view:nullAndroid DataBinding —— view 标签不正确 view:null
【发布时间】:2016-05-07 02:28:31
【问题描述】:

我的班级扩展了LinearLayout,我使用DataBinding 来扩充布局。但是代码会抛出一个异常,即 view:null 上的 view 标签不正确。

这是我的代码:

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

     }
}

布局文件是:

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

【问题讨论】:

  • 有一个类似的问题,这里有你的确切错误。 stackoverflow.com/questions/33116688/…
  • 我看过了。但它不起作用。我在自定义视图类中使用dataBinding,不知道对不对
  • 我无法重现这个。正在使用哪个版本的 Android gradle 插件?我用 Android Studio 2.1 和它附带的插件进行了测试。也有可能您正在扩充非绑定布局文件。

标签: android data-binding


【解决方案1】:

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

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

在此之后,您可以在不丢失预览功能的情况下绑定视图。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 我现在正面临这个问题。它在运行时有效,但在预览中无效。现在有什么办法解决这个问题吗?
    • 一年过去了,我在使用 Android Studio 3.0.1 时遇到了这个问题。就我而言,我有一个使用数据绑定的自定义视图,该视图在数据绑定片段中多次使用。我想我会使用老式的按 id 查找方式从视图中删除绑定,并在此时继续仅在片段上使用绑定。
    【解决方案3】:

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

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

    请阅读isInEditMode 是什么:Android dev docs

    【讨论】:

      猜你喜欢
      • 2016-01-12
      • 1970-01-01
      • 2013-06-29
      • 2016-07-14
      • 2018-12-25
      • 2023-03-17
      • 1970-01-01
      相关资源
      最近更新 更多