数据绑定嵌套的自定义布局

时间:2017-02-22 08:27:17

标签: android data-binding android-databinding

我试图尽可能多地实现DataBinding。目前,我正在使用它,就像我和ButterKnife一样,但是将来我实际上会绑定一些数据。我已经成功地使用以下SSCCE重现了这个问题:

CustomLayout.java

public class CustomLayout extends LinearLayout {

  CustomLayoutBinding mBinding;

  public CustomLayout(Context context) {
      this(context, null, 0);
  }

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

  public CustomLayout(Context context, AttributeSet attrs, int defStyleAttr) {
      super(context, attrs, defStyleAttr);
      init(context);
  }

  private void init(Context context) {
      LayoutInflater inflater = LayoutInflater.from(context);
      mBinding = CustomLayoutBinding.inflate(inflater, this, true);
  }
}

custom_layout.xml

<layout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <TextView
        android:id="@+id/titleTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:text="Test text"/>

    <ImageView
        android:id="@+id/iconImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:src="@android:drawable/btn_star"/>
</LinearLayout>
</layout>

我的想法是,我可以轻松地重复使用这种布局,保持模块化。现在,当我创建以下活动时出现问题:

CustomActivity.java

public class CustomActivity extends AppCompatActivity {

  ActivityCustomBinding mBinding;

  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      mBinding = DataBindingUtil.setContentView(CustomActivity.this, R.layout.activity_custom);
  }
}

activity_custom.xml

<layout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">

<android.support.constraint.ConstraintLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <es.test.sample.CustomLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
</layout>

在这种情况下,应用程序因以下异常而崩溃:

java.lang.RuntimeException: view tag isn't correct on view:null
at es.test.sample.databinding.CustomLayoutBinding.bind(CustomLayoutBinding.java:98)
at android.databinding.DataBinderMapper.getDataBinder(DataBinderMapper.java:27)
at android.databinding.DataBindingUtil.bind(DataBindingUtil.java:185)
at android.databinding.DataBindingUtil.bindToAddedViews(DataBindingUtil.java:299)
at android.databinding.DataBindingUtil.inflate(DataBindingUtil.java:118)
at es.test.sample.databinding.CustomLayoutBinding.inflate(CustomLayoutBinding.java:85)
at es.test.sample.databinding.CustomLayoutBinding.inflate(CustomLayoutBinding.java:82)
at es.test.sample.CustomLayout.init(CustomLayout.java:34)
at es.test.sample.CustomLayout.<init>(CustomLayout.java:28)
at es.test.sample.CustomLayout.<init>(CustomLayout.java:23)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:465)
at org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:172)
at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:105)
at com.android.tools.idea.rendering.LayoutlibCallbackImpl.loadView(LayoutlibCallbackImpl.java:186)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:334)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:345)
at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:245)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:858)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:70)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:834)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:324)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:429)
at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:389)
at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:548)
at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:533)
at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:966)
at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:533)
at com.android.tools.idea.rendering.RenderTask.lambda$inflate$70(RenderTask.java:659)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

我已经看到了关于这个观点的其他一些问题:null的东西,但它们都没有为我工作。具体来说,我尝试过以下方法:

CustomLayout.class

public class CustomLayout extends LinearLayout {

  CustomLayoutBinding mBinding;

  public CustomLayout(Context context) {
      this(context, null, 0);
  }

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

  public CustomLayout(Context context, AttributeSet attrs, int defStyleAttr) {
      super(context, attrs, defStyleAttr);
      init(context);
  }

  private void init(Context context) {
      LayoutInflater inflater = LayoutInflater.from(context);
      inflater.inflate(R.layout.custom_layout, this, true);
  }

  @Override
  protected void onLayout(boolean changed, int l, int t, int r, int b) {
      super.onLayout(changed, l, t, r, b);
      mBinding = CustomLayoutBinding.bind(getRootView());
  }
}

它无法解决此问题。

我在这里做错了什么?

谢谢!

0 个答案:

没有答案