databinding msg在kotlin数据类中找不到访问器



错误消息

Found data binding error(s):
[databinding] {"msg":"Could not find accessor com.dubhe.room.entity.User.name","file":"app\src\main\res\layout\activity_add_user.xml","pos":[{"line0":31,"col0":28,"line1":31,"col1":36}]}

我的布局XML

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import
alias="user"
type="com.dubhe.room.entity.User" />
<variable
name="add"
type="android.view.View.OnClickListener" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:title="addUser" />
<EditText
android:id="@+id/editUserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="name"
android:text="@{user.name}" />     <-error in this line.
<EditText
android:id="@+id/editUserAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="age"
android:inputType="number"
android:text="@{user.age}" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="@{add}"
android:text="Add" />
</LinearLayout>
</layout>

我的实体是一个kotlin数据类。

@Entity(tableName = "user")
data class User(
@PrimaryKey(autoGenerate = true) @ColumnInfo(name = "user_id") var id: Int = 0,
@ColumnInfo(name = "user_name")var name: String = "",
@ColumnInfo(name = "user_age")var age: Int = 0,
@Ignore var isChecked: Boolean = false
)

build.gradle在app目录中。

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "com.dubhe.databinding"
minSdkVersion 23
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
dataBinding {
enabled = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'com.google.android.material:material:1.1.0-alpha08'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
//    implementation 'com.android.support:support-vector-drawable:29.0.0'
implementation 'androidx.core:core-ktx:1.0.2'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
//Room
implementation 'android.arch.persistence.room:runtime:2.1.4'
//BaseRecyclerViewAdapter
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.46'
annotationProcessor 'android.arch.persistence.room:compiler:2.1.4'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9'
}

无论是清理并重建缓存还是使缓存无效,我都无法编译。

您使用的是import而不是variable:

此:

<import alias="user" type="com.dubhe.room.entity.User" />

应该是这样的:

<variable name="user" type="com.dubhe.room.entity.User" />

最新更新