简体   繁体   中英

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. I use data binding and works fine by adding

dataBinding { enabled = true }

in each module build.gradle.

The problem occurred when I use <include> tag in the layout of module A . It crashing when calling setContentView from DataBindingUtil.

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.package.name.databinding.ViewToolbarBinding.invalidateAll()' on a null object reference

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 :

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

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

@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 :

   @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:

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. So if you use the DataBindingUtil.inflate you should be safe. You should also note that any of the UI setup should be done in the onCreateView() rather than onCreate() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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