Listener Binding; Cannot Find the Setter

Bryan picture Bryan · Jun 16, 2017 · Viewed 24.8k times · Source

I am trying to implement listener bindings, but when I run my code I get the following error:

Caused by: android.databinding.tool.util.LoggedErrorException: Found data binding errors.
****/ data binding error ****msg:Cannot find the setter for attribute 'android:onClick' with parameter type lambda on android.widget.Button. file:~/GithubBrowser/app/src/main/res/layout/loading_state.xml loc:30:31 - 30:52 ****\ data binding error ****

This is the layout file in question:

<?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 type="com.example.app.data.model.Resource"/>
        <import type="com.example.app.data.model.Status"/>
        <variable name="resource" type="Resource"/>
        <variable name="callback" type="com.example.app.ui.common.RetryCallback"/>
    </data>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center"
        android:padding="@dimen/default_margin">

        <Button android:id="@+id/retry"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/retry"
            android:onClick="@{() -> callback.retry()}"/>

    </LinearLayout>

</layout>

And this is the RetryCallback interface referenced in the layout:

package com.example.app.ui.common

interface RetryCallback {

    fun retry()

}

Edit

The top-level build.gradle:

buildscript {
    ext.android_tools_version = '3.0.0-alpha3'
    ext.kotlin_version = '1.1.2-5'
    repositories {
        maven { url 'https://maven.google.com' }
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:$android_tools_version"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

ext {
    architecture_version = '1.0.0-alpha2'
    constraint_version = '1.0.2'
    dagger_version = '2.11'
    espresso_version = '2.2.2'
    glide_version = '3.7.0'
    junit_version = '4.12'
    mockito_version = '2.7.19'
    mock_server_version = '3.6.0'
    moshi_version = '1.5.0'
    retrofit_version = '2.2.0'
    support_version = '25.4.0'
    timber_version = '4.5.1'
}

allprojects {
    repositories {
        jcenter()
        mavenCentral()
        maven { url 'https://maven.google.com' }
    }
}

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

And the app module build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.3"
    defaultConfig {
        applicationId "com.example.app"
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        debug {
            testCoverageEnabled !project.hasProperty('android.injected.invoked.from.ide')
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    dataBinding {
        enabled = true
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

kapt {
    generateStubs = true
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"

    compile "com.android.support:appcompat-v7:$support_version"
    compile "com.android.support:cardview-v7:$support_version"
    compile "com.android.support:design:$support_version"
    compile "com.android.support:recyclerview-v7:$support_version"
    compile "com.android.support.constraint:constraint-layout:$constraint_version"

    compile "android.arch.lifecycle:extensions:$architecture_version"
    compile "android.arch.lifecycle:runtime:$architecture_version"
    compile "android.arch.persistence.room:runtime:$architecture_version"

    compile "com.google.dagger:dagger:$dagger_version"
    compile "com.google.dagger:dagger-android:$dagger_version"
    compile "com.google.dagger:dagger-android-support:$dagger_version"

    compile "com.squareup.moshi:moshi:$moshi_version"
    compile "com.squareup.retrofit2:retrofit:$retrofit_version"
    compile "com.squareup.retrofit2:converter-moshi:$retrofit_version"

    compile "com.github.bumptech.glide:glide:$glide_version"

    compile "com.jakewharton.timber:timber:$timber_version"

    kapt "com.android.databinding:compiler:$android_tools_version"
    kapt "com.google.dagger:dagger-android-processor:$dagger_version"
    kapt "com.google.dagger:dagger-compiler:$dagger_version"
    kapt "android.arch.persistence.room:compiler:$architecture_version"
    kapt "android.arch.lifecycle:compiler:$architecture_version"

    testCompile "junit:junit:$junit_version"
    testCompile "com.squareup.okhttp3:mockwebserver:$mock_server_version"
    testCompile ("android.arch.core:core-testing:$architecture_version", {
        exclude group: 'com.android.support', module: 'support-compat'
        exclude group: 'com.android.support', module: 'support-annotations'
        exclude group: 'com.android.support', module: 'support-ccore-utils'
    })

    androidTestCompile "com.android.support:appcompat-v7:$support_version"
    androidTestCompile "com.android.support:cardview-v7:$support_version"
    androidTestCompile "com.android.support:design:$support_version"
    androidTestCompile "com.android.support:recyclerview-v7:$support_version"

    androidTestCompile ("com.android.support.test.espresso:espresso-core:$espresso_version", {
        exclude group: 'com.android.support', module: 'support-annotations'
        exclude group: 'com.google.code.findbugs', module: 'jsr305'
    })

    androidTestCompile ("com.android.support.test.espresso:espresso-contrib:$espresso_version", {
        exclude group: 'com.android.support', module: 'support-annotations'
        exclude group: 'com.google.code.findbugs', module: 'jsr305'
    })

    androidTestCompile "org.mockito:mockito-android:$mockito_version"
}

Answer

Matej Drobnič picture Matej Drobnič · Jul 8, 2017

I just had that issue and I have managed to solve it by deleting .idea, .gradle and gradle folders and let Android Studio recreate whole project from scratch from gradle files.