[英]Cannot find the setter for attribute in Data binding
I am using LiveData, DataBinding, and Kotlin in my application.我在我的应用程序中使用了 LiveData、DataBinding 和 Kotlin。
I defined a Binding Adapter for a RecyclerView like this:我为 RecyclerView 定义了一个绑定适配器,如下所示:
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)
}
}
}
}
and my reyclerView in XML assigned to this bindingAdapter like this:和我的 XML 中的 reyclerView 分配给这个 bindingAdapter ,如下所示:
<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"/>
And in my ViewModel, I used在我的 ViewModel 中,我使用了
val items = MutableLiveData<List<Homeitem>>()
to create the list.创建列表。
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 ****
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 plugin: 'kotlin-kapt'
And apply plugin: 'kotlin-android-extensions'
。
After adding these plugins you have to replaced your annotationProcessors
with kapt
.添加这些插件后,您必须将
annotationProcessors
替换为kapt
。
After that, every thing might be going well.在那之后,一切可能都会顺利。
Binding Adapter绑定适配器
@JvmStatic
@BindingAdapter("main_items")
fun setItems(recyclerView: RecyclerView, items: MutableLiveData<List<String>>) {
}
Model模型
class User : ViewModel(){
lateinit var list: MutableLiveData<List<String>>
}
Bind Data绑定数据
val items = ViewModelProviders.of(this@FragmentName)
.get(RecyclerViewHelper.User::class.java)
mBinding!!.user = items
Layout布局
<layout>
<data>
<variable
name="user"
type="com.XXX.view.recylerview.RecyclerViewHelper.User" />
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/helper_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
app:main_items="@{user.list}" />
</RelativeLayout>
</layout>
检查@BindingAdapter 的值后是否有任何空格,如下所示:@BindingAdapter("sleepDurationFormatted ") 花了我 2 小时才找到。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.