android databinding - cannot find class android.vi

2020-04-15 15:39发布

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

java.lang.ClassNotFoundException: Didn't find class "android.view.data"

My layout file looks like this:

<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>

I updated my build.gradle file with following lines:

dataBinding {
    enabled = true
}

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

2条回答
太酷不给撩
2楼-- · 2020-04-15 15:55

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>
查看更多
我想做一个坏孩纸
3楼-- · 2020-04-15 16:04

The data binding is never in the <LinearLayout>. You should put it in the the <layout> zone like this:

<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>
查看更多
登录 后发表回答