ClassNotFoundException: 未找到 class "android.view.data"

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

这是我的 xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/a"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.a.AFragment">
    <data>
        <import type="android.databinding.ObservableArrayMap"/>
        <variable name="user" type="ObservableArrayMap"/>
    </data>
    <TextView
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@{user.name}"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

我在 运行 上收到以下错误:

Caused by: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class data Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class data Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.data" on path: DexPathList[[zip file "/data/app/x.x.x-2/base.apk", zip file "/data/app/x.x.x-2/split_lib_dependencies_apk.apk", zip file "/data/app/x.x.x-2/split_lib_slice_0_apk.apk", zip file "/data/app/x.x.x-2/split_lib_slice_1_apk.apk", zip file "/data/app/x.x.x-2/split_lib_slice_2_apk.apk", zip file "/data/app/x.x.x-2/split_lib_slice_3_apk.apk", zip file "/data/app/x.x.x-2/split_lib_slice_4_apk.apk", zip file "/data/app/x.x.x-2/split_lib_slice_5_apk.apk", zip file "/data/app/x.x.x-2/split_lib_slice_6_apk.apk", zip file "/data/app/x.x.x-2/split_lib_slice_7_apk.apk", zip file "/data/app/x.x.x-2/split_lib_slice_8_apk.apk", zip file "/data/app/x.x.x-2/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/x.x.x-2/lib/arm, /system/lib, /vendor/lib]]

错误在 <data> 行(开始标记)。

是什么导致了这个错误,我该如何解决?

Data binding layout files are slightly different and start with a root tag of layout followed by a data element and a view root element

https://developer.android.com/topic/libraries/data-binding/expressions

您没有通用布局标签

这是sample layout文件。相应地修改您的布局。

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
   <data>
       <variable name="user" type="com.example.User"/>
   </data>
   <LinearLayout
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
       <TextView android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@{user.firstName}"/>
       <TextView android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@{user.lastName}"/>
   </LinearLayout>
</layout>

Android

中的dataBinding需要使用以下内容

首先确保,您正在为模型添加 build.gradle 中的 dataBinding

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "YOUR_APPP"
        minSdkVersion 15
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"

    }

    dataBinding {
        enabled = true   //// This is need to be for the dataBinding
    }
    buildToolsVersion '27.0.3'
}

你的build.gradle(项目)中的第二个需要使用repositories[=20=中的jcenter()条目]

    allprojects {
        repositories {
            google()
            jcenter()
            maven { url "https://jitpack.io" }

        }
    }

    task clean(type: Delete) {
      delete rootProject.buildDir
    }

layout.xml 中的最后一个,您需要 startend 标签使用 layout 标签如下

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">



    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:padding="10dp"
        android:background="@drawable/splash"
        tools:context=".user.activity.SplashActivity">


         /**
          * USE YOUR ALL RESOURCES HERE
          */

    </RelativeLayout>
</layout>

要了解更多信息,请参阅此站点 Reference site