视图标记在视图上不正确:null

时间:2016-04-28 00:46:58

标签: android data-binding dragger

我使用Android数据绑定并使用Dragger问题就是当我这样做时

MyLayoutbinding = DataBindingUtil.setContentView(this, R.layout.my_Layout);

我收到了错误

Caused by: java.lang.RuntimeException: view tag isn't correct on view:null                                                                          
   at com.rkmax.myapp.databinding.MyLayoutBinding.bind(MyLayoutBinding.java:191)
   at android.databinding.DataBinderMapper.getDataBinder(DataBinderMapper.java:15)
   at android.databinding.DataBindingUtil.bind(DataBindingUtil.java:185)
   at android.databinding.DataBindingUtil.bindToAddedViews(DataBindingUtil.java:299)
   at android.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:279)
   at android.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:261)
   at com.rkmax.myapp.MyActivity.onCreate(MyActivity.java:26)

my_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <import type="android.graphics.drawable.Drawable" />
        <variable name="indicatorBg" type="Drawable" />
        <variable name="categoryName" type="String" />
        <variable name="progress" type="int" />
    </data>

    <RelativeLayout android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/bg_blackboard">

        <ProgressBar
                    style="?android:attr/progressBarStyleHorizontal"
                    android:layout_width="match_parent"
                    android:layout_height="15.3dp"
                    android:id="@+id/progressBar2"
                    android:max="10"
                    android:progress="@{progress}" />


    </RelativeLayout>
</layout>

1 个答案:

答案 0 :(得分:1)

经过大量的反复试验,我所做的就是将DraggerActivity源复制到并创建自己的DraggerActivity,然后添加以下方法

private void configViews(View root) {
    this.draggerPanel.addViewOnDrag(root);
    if(this.shadowResID == -1) {
        this.shadowResID = com.github.ppamorim.dragger.R.layout.layout_shadow;
    }

    this.draggerPanel.addViewOnShadow(this.inflateLayout(this.shadowResID));
    super.setContentView(this.draggerPanel);
}


public void setContentView(View root) {
    this.configDraggerView();
    this.configViews(root);
    super.setContentView(this.draggerPanel);
}

然后在onCreate方法

View rootView = getLayoutInflater().inflate(R.layout.my_Layout, null, false);
mBinding = DataBindingUtil.bind(rootView);

setContentView(rootView);

通过这种方式,我可以将数据绑定到我的DraggerActivity