In meinem Projekt habe ich mehrere Module, sagen wir Modul-App und Modul A, die als eine Bibliothek für Modul-App fungieren. Ich benutze die Datenbindung und Werke von feinenAndroid Datenbindung mit <include> Tag in der Bibliothek Modul
dataBinding { enabled = true }
in jedem Modul build.gradle hinzufügen.
Das Problem trat auf, wenn ich <include>
Tag im Layout von Modul A verwenden. Es stürzt beim Aufruf von setContentView von DataBindingUtil ab.
java.lang.NullPointerException: Der Versuch, virtuelle Methode 'Leere com.package.name.databinding.ViewToolbarBinding.invalidateAll()' auf ein Null-Objekt Verweis aufrufen
aber es funktioniert gut in der Modul-App kann ich auf die Ansicht zugreifen, indem ich so etwas verwende.
mBindingUtil.includedLayout.viewInTheIncludedLayout
Dies ist meine Tätigkeit Layout in Modul 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>
Und das ist mein view_toolbar.xml in Modul 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>
Während das ist, wie ich die Aktivität aufblasen in Modul A:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBinding = DataBindingUtil.setContentView(this, R.layout.activity_with_include);
}
Jede Hilfe wird geschätzt. Dank
Vielen Dank. Dies funktioniert jedoch nur bei Fragmenten. Ich habe es auf Aktivität versucht und es funktioniert nicht. – luthfihariz