我试图在我的android应用程序中实现数据绑定,但是我遇到了这个问题:
java.lang.ClassNotFoundException: Didn't find class "android.view.data"
我的布局文件如下所示:
<layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.myapp.views.fragments.LocationSearchFragment"> <!-- data setup --> <data> <variable name="location" type="com.myapp.models.Address" /> </data> </LinearLayout> </layout>
我build.gradle用以下几行更新了文件:
build.gradle
dataBinding { enabled = true }
如文档所建议:https : //developer.android.com/topic/libraries/data- binding/index.html。我正在运行最新版本的Android Studio。
您需要将data定义放在您的之外LinearLayout:
data
LinearLayout
<layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <!-- data setup --> <data> <variable name="location" type="com.myapp.models.Address" /> </data> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.myapp.views.fragments.LocationSearchFragment"> </LinearLayout> </layout>