Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listener Binding; Cannot Find the Setter

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"
}
like image 902
Bryan Avatar asked Jun 16 '17 17:06

Bryan


4 Answers

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.

2022 UPDATE: Do not delete gradle folder, only .gradle and .idea.

like image 189
Matej Drobnič Avatar answered Nov 14 '22 10:11

Matej Drobnič


Make sure apply plugin: 'kotlin-kapt' is in build.gradle.

If you are running in to this and no amount of rebuilding the project, deleting directories, clearing AS settings do anything. The IDE is fine without having it but the building fails.

It would be nice if android studio had better error messages for this.

like image 23
kevswanberg Avatar answered Nov 14 '22 11:11

kevswanberg


Just rebuild the project. It is probably because you did some refactoring

like image 1
Mladen Rakonjac Avatar answered Nov 14 '22 11:11

Mladen Rakonjac


Valid case: This error is raised if you have an error in the xml file. First read carefully through the xml and if you find a problem fix it.

Invalid case: For me the issue was raised for a file that was already deleted from the project or the file was renamed and the databinding was referencing the old file.

I'm not sure how to get rid of the issue every time but I have a number of things that sometimes work and sometimes don't.

Fix number 1:

Running 'gradle wrapper' in the project root.

Fix number 2:

Android Studio Clean Project

Next delete these folders in the project root:

rm -rf .gradle/

rm -rf app/build/

rm -rf build

rm -rf gradle

rm -rf gradlew

Android Studio Invalidate and restart

Everything should be back to normal now.

like image 1
noev Avatar answered Nov 14 '22 11:11

noev