Cannot find the getter for attribute 'android:checked'

928 Views Asked by At

data binding error ****msg:Cannot find the getter for attribute 'android:checked' with value type java.lang.Boolean on android.widget.CheckedTextView.

I have a Kotlin Android app and one of the XML layouts contains a CheckedTextView and I want to two-way bind the checked property to the checked value of the ViewModel. The idea is that the checked property in the viewModel will represent the one on the view itself. This fails with the error message above. Now I wonder whether this is because checked is a boolean value and the getter is called isChecked. Can Databinding not recognize that? So I tried extending it with a getChecked function, but that didn't resolve the error. Maybe because while Kotlin supports extension functions, Java does not. Any ideas how this can be solved?

XML file:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable name="viewModel"
            type="lehrbaum.de.onenightcomps.view.SimpleCheckableListItemViewModel"/>
    </data>
    <CheckedTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView"
        android:padding="@dimen/text_margin"
        android:gravity="center_vertical"
        android:textStyle="bold"
        android:checkMark="?android:attr/listChoiceIndicatorSingle"
        android:checkMarkTint="@color/colorPrimary"
        android:checked="@={viewModel.checked}"
        android:text="@{viewModel.text}"/>
</layout>

ViewModel class:

class SimpleCheckableListItemViewModel {
    val checked : MutableLiveData<Boolean> = MutableLiveData()
    val text : MutableLiveData<String> = MutableLiveData()
}

Extension function:

fun CheckedTextView.getChecked(): Boolean {
    return this.isChecked
}
1

There are 1 best solutions below

0
Ehsan On

There might be different reasons for this error but in my case, the problem raised up because I didn't add apply plugin: 'kotlin-kapt' And apply plugin: 'kotlin-android-extensions' in my Gradle.

After adding these plugins you have to replaced your annotationProcessors with kapt.

After that, every thing might be going well.