如何在我的 android 项目中正确启用 android.databinding 库?
how can I properly enable android.databinding library in my android project?
我想使用 ObservableList,所以我遵循 this instruction,在我的 (Module:app)build.gradle 和 Syns:
中添加下面的代码
android {
...
buildFeatures {
dataBinding true
}
}
是的,它有效。我现在可以将 ObservableList 导入我的 class。但问题是,当我 运行 应用程序时,出现编译异常(NullPointException):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="0dp">
<com.example.lj032.myapplication.MyGridView
android:id="@+id/grid_photo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/LinearLayout1"
android:numColumns="4"
android:layout_alignParentStart="true" />
</RelativeLayout>
很明显,根据日志,问题是由数据绑定库引起的。但是我不知道怎么解决这个问题。
How can I properly Enable dataBinding
里面Build.gradle(:app)
dataBinding {
enabled = true
}
依赖关系:
implementation 'com.android.databinding:compiler:3.5.1'
像这样绑定你的 xml :
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="item"
type="DataModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:text="@{item.name}"/>// binding data from model
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
P.S: 不要忘记在Build.gradle(:app)
中添加插件
apply plugin: 'kotlin-kapt'
完成上述所有步骤后同步并重建项目
我想使用 ObservableList,所以我遵循 this instruction,在我的 (Module:app)build.gradle 和 Syns:
中添加下面的代码android {
...
buildFeatures {
dataBinding true
}
}
是的,它有效。我现在可以将 ObservableList 导入我的 class。但问题是,当我 运行 应用程序时,出现编译异常(NullPointException):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="0dp">
<com.example.lj032.myapplication.MyGridView
android:id="@+id/grid_photo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/LinearLayout1"
android:numColumns="4"
android:layout_alignParentStart="true" />
</RelativeLayout>
很明显,根据日志,问题是由数据绑定库引起的。但是我不知道怎么解决这个问题。
How can I properly Enable dataBinding
里面Build.gradle(:app)
dataBinding {
enabled = true
}
依赖关系:
implementation 'com.android.databinding:compiler:3.5.1'
像这样绑定你的 xml :
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="item"
type="DataModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:text="@{item.name}"/>// binding data from model
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
P.S: 不要忘记在Build.gradle(:app)
apply plugin: 'kotlin-kapt'
完成上述所有步骤后同步并重建项目