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

Viewed 2455

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

1 Answers
Related