android - 数据绑定(bind) - 必须首先将 Lambda 表达式解析为其 setter 以获取类型

标签 android kotlin mvvm android-databinding

我从数据绑定(bind)开始,但一旦创建 onClick 就无法运行该项目功能,这是我的layout

<layout 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">
    <data>
        <variable
            name="userViewModel"
            type="com.myproject.example.UserViewModel" />
    </data>
    ....
    <Button
                android:id="@+id/btnSave"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@={userViewModel.saveOrUpdateButton}"
                android:onClick="@={() -> userViewModel.saveOrUpdate()}"
                 />


仅在运行 ./gradlew :app:kaptDebugKotlin --stacktrace 时出现的问题它指向 onClick() 方法

还有这个

Caused by: org.gradle.workers.internal.DefaultWorkerExecutor$WorkExecutionException: A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution

Caused by: org.jetbrains.kotlin.kapt3.base.util.KaptBaseError: Exception while annotation processing



我的 build.gradle包含:

apply plugin: 'kotlin-kapt'

dataBinding { enabled = true }



和依赖项:
dependencies {
    def lifecycle_version = "2.2.0"
    def room_version = "2.2.3"
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

    // ViewModel
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
    // LiveData
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
    // Annotation processor
    kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"

    implementation "androidx.room:room-runtime:$room_version"
    kapt "androidx.room:room-compiler:$room_version"

    // optional - Kotlin Extensions and Coroutines support for Room
    implementation "androidx.room:room-ktx:$room_version"

    //coroutines
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.3'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.3'

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'androidx.recyclerview:recyclerview:1.0.0'
    implementation 'androidx.cardview:cardview:1.0.0'
}

我错过了什么吗?

最佳答案

@=用于two-way data-binding , 对于 onClick你应该使用 @

android:onClick="@{() -> userViewModel.saveOrUpdate()}"

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

关于android - 数据绑定(bind) - 必须首先将 Lambda 表达式解析为其 setter 以获取类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61572475/

相关文章:

ios - 如何在 swift 中使用 MVVM 和 RxSwift 显示/隐藏 progressHUD

android - 在 CoordinatorLayout 的工具栏下方添加 View

java - 如何在全屏 Canvas 上绘制位图?

android - 无法在 Kotlin 中构建发布版本

android - Android Studio 更新到 0.6 后 Kotlin 出现问题

mvvm - kendo 将 HTML 元素绑定(bind)到网格选定的行/数据项

android - 如何使用Kotlin DSL Gradle启用代码覆盖?

android - 如何使用 Android 从 json 对象获取最大值/最大值?

generics - Kotlin 泛型类属性

wpf - 没有 PRISM 的复合应用程序