Android数据绑定-找不到类android.view.data

Android数据绑定-找不到类android.view.data

问题描述:

我试图在我的android应用中实现数据绑定,但是我遇到了这个问题:

I'm trying to implement databinding in my android app, however I'm stuck with this issue:

java.lang.ClassNotFoundException :找不到类 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 文件以下行:

I updated my build.gradle file with following lines:

dataBinding {
    enabled = true
}

如文档所建议: https://developer.android.com/topic/libraries/data-binding/index.html 。我正在运行最新版本的Android Studio。

As the documentation suggested: https://developer.android.com/topic/libraries/data-binding/index.html . I'm running the most recent version of Android Studio.

您需要放入 data 定义在 LinearLayout 之外:

You need to put your data definition outside of your 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>