简体   繁体   English

Android数据绑定 <include> 库模块中的标签

[英]Android data binding with <include> tag in the library module

In my project I have multiple modules, let's say module app and module A that act as a library for module app. 在我的项目中,我有多个模块,假设模块应用程序和模块A充当模块应用程序的库。 I use data binding and works fine by adding 我使用数据绑定,并通过添加来正常工作

dataBinding { enabled = true }

in each module build.gradle. 在每个模块build.gradle中。

The problem occurred when I use <include> tag in the layout of module A . 当我在模块A的布局中使用<include>标记时,发生了问题。 It crashing when calling setContentView from DataBindingUtil. 从DataBindingUtil调用setContentView时崩溃。

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.package.name.databinding.ViewToolbarBinding.invalidateAll()' on a null object reference java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ void com.package.name.databinding.ViewToolbarBinding.invalidateAll()”

However It works fine in the module app, I am able to access the view by using something like this. 但是,它在模块应用程序中工作正常,我可以使用类似方法访问视图。

mBindingUtil.includedLayout.viewInTheIncludedLayout

This is my activity layout in module A : 这是我在模块A中的活动布局:

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

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

    <include layout="@layout/view_toolbar"
        android:id="@+id/toolbar_layout"/>

</LinearLayout>

</layout>

And this is my view_toolbar.xml in module A : 这是我在模块A中的view_toolbar.xml:

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

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    style="@style/ToolbarTheme"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/primary_blue"
    android:theme="@style/AppTheme"
    app:elevation="0dp" />

</layout>

While this is how I inflate the activity in module A : 这就是我如何在模块A中增加活动:

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

Any help is appreciated. 任何帮助表示赞赏。 Thanks 谢谢

You should inflate a view that is using data binding by using DataBindingUtil.inflate instead of DataBindingUtil.setContentView : 您应该通过使用DataBindingUtil.inflate而不是DataBindingUtil.setContentView为使用数据绑定的视图充气:

   @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.yourlayoutfile, container, false);
        //viewmodel assigned in oncreate()
        mBinding.setViewModel(yourViewModel);
        return mBinding.getRoot();
    }

If you look at the docs , you will see that the method for DataBindingUtil.setContentView says: 如果您看一下docs ,将会看到DataBindingUtil.setContentView的方法说:

Set the Activity's content view to the given layout and return the associated binding. 将活动的内容视图设置为给定的布局,并返回关联的绑定。 The given layout resource must not be a merge layout. 给定的布局资源不得为合并布局。

I think it safe to assume that this includes include tags. 我认为可以肯定地认为其中include标签。 So if you use the DataBindingUtil.inflate you should be safe. 因此,如果使用DataBindingUtil.inflate ,则应该是安全的。 You should also note that any of the UI setup should be done in the onCreateView() rather than onCreate() . 您还应该注意,任何UI设置都应该在onCreateView()而不是onCreate()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM