简体   繁体   中英

Databinding - Lambda expression must be resolved to its setter first to get the type

I'm starting with Databinding, but I can not run the project once I create an onClick function, this is my 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()}"
                 />

The issue that it appears only when I run ./gradlew:app:kaptDebugKotlin --stacktrace it points on the onClick() mehtod

Also this

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

My build.gradle contains:

apply plugin: 'kotlin-kapt'

dataBinding { enabled = true }

And the dependencies:

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

Is something I'm missing?

@= is used for two-way data-binding , for onClick you should use @

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM