无法为Android数据绑定创建自定义设置器-“找不到访问器”错误
作者:互联网
我正在尝试按以下说明创建自定义设置器:
https://developer.android.com/tools/data-binding/guide.html#custom_setters
我的看法是:
<data>
<variable name="viewModel"
type="com.example.feed.DummyViewModel" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:imageUrl="@{viewModel.imageUrl}" />
</LinearLayout>
</layout>
绑定是:
package com.example.feed;
import android.databinding.BaseObservable;
import android.databinding.BindingAdapter;
import android.widget.ImageView;
public class DummyViewModel extends BaseObservable {
@BindingAdapter("bind:imageUrl")
public static void loadImage(ImageView view, String url) {
// nothing yet
}
}
文楼我得到错误:
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> java.lang.RuntimeException: Found data binding errors.
****/ data binding error ****msg:Could not find accessor com.example.feed.DummyViewModel.imageUrl file:/Users/tse/t/app/src/main/res/layout/fragment_main.xml loc:20:28 - 20:45 ****\ data binding error ****
我的loadImage函数有什么问题?如何制作正确的绑定适配器?
解决方法:
找不到访问器com.example.feed.DummyViewModel.imageUrl
DummyViewModel don’t have get method: getImageUrl()
您也可以定义public:
public String imageUrl
标签:android-databinding,android 来源: https://codeday.me/bug/20191027/1948298.html