[英]Cannot find the getter for attribute 'android:checked'
data binding error ****msg:Cannot find the getter for attribute 'android:checked' with value type java.lang.Boolean on android.widget.CheckedTextView.
数据绑定错误**** msg:在android.widget.CheckedTextView上找不到类型为java.lang.Boolean的属性'android:checked'的获取方法。
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. 我有一个Kotlin Android应用程序,其中一个XML布局包含一个
CheckedTextView
并且我想将选中的属性双向绑定到ViewModel的选中值。 The idea is that the checked property in the viewModel will represent the one on the view itself. 这个想法是viewModel中的checked属性将代表视图本身上的属性。 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
. 现在我想知道这是否是因为check是一个布尔值,而该getter称为
isChecked
。 Can Databinding not recognize that? 数据绑定无法识别吗? So I tried extending it with a
getChecked
function, but that didn't resolve the error. 因此,我尝试使用
getChecked
函数对其进行getChecked
,但是并不能解决该错误。 Maybe because while Kotlin supports extension functions, Java does not. 可能是因为虽然Kotlin支持扩展功能,但Java不支持。 Any ideas how this can be solved?
有什么想法可以解决吗?
XML file: XML档案:
<?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: ViewModel类:
class SimpleCheckableListItemViewModel {
val checked : MutableLiveData<Boolean> = MutableLiveData()
val text : MutableLiveData<String> = MutableLiveData()
}
Extension function: 扩展功能:
fun CheckedTextView.getChecked(): Boolean {
return this.isChecked
}
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. 发生此错误的原因可能不同,但就我而言,问题是由于我没有在Gradle中添加Apply插件:'kotlin-kapt'和应用插件:'kotlin-android-extensions'而引起的。
After adding these plugins you have to replaced your annotationProcessors with kapt. 添加这些插件后,您必须用kapt替换您的注解处理器。
After that, every thing might be going well. 在那之后,所有事情可能都会进展顺利。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.