问题描述
我在应用程序中使用LiveData,DataBinding和Kotlin.
I am using LiveData, DataBinding, and Kotlin in my application.
我为RecyclerView定义了一个绑定适配器,如下所示:
I defined a Binding Adapter for a RecyclerView like this:
class MainListBindings {
private val TAG = "TasksListBindings"
companion object {
@JvmStatic
@SuppressWarnings("unchecked")
@BindingAdapter("main_items")
fun setItems(recyclerView: RecyclerView, items: MutableLiveData<List<Homeitem>>? = null) {
val adapter: RecyclerMainAdapter = recyclerView.adapter as RecyclerMainAdapter
//prevent use of null list
items?.let {
adapter.swapData(items)
}
}
}
}
和我分配给此bindingAdapter的XML格式的reyclerView:
and my reyclerView in XML assigned to this bindingAdapter like this:
<android.support.v7.widget.RecyclerView
android:id="@+id/main_recycler"
app:main_items="@{viewmodel.items}"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
在我的ViewModel中,我使用
And in my ViewModel, I used
val items = MutableLiveData<List<Homeitem>>()
创建列表.
但是在构建应用程序时出现此错误:
But I am getting this error at building Application:
Found data binding errors.
****/ data binding error ****msg:Cannot find the setter for attribute 'app:main_items' with parameter type android.arch.lifecycle.MutableLiveData<java.util.List<project.data.pojo.Homeitem>> on android.support.v7.widget.RecyclerView. file:/home/user/Yara/bazinama_mag/bazinama/app/src/main/res/layout/fragment_main.xml loc:22:24 - 22:38 ****\ data binding error ****
推荐答案
此错误可能有不同的原因,但就我而言,出现此问题是因为我没有在Gradle
.
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
.
添加这些插件后,您必须将annotationProcessors
替换为kapt
.
After adding these plugins you have to replaced your annotationProcessors
with kapt
.
在那之后,一切都会顺利进行.
After that, every thing might be going well.
这篇关于在数据绑定中找不到属性的设置器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!