我正在尝试按照此处所述创建自定义 setter : 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>
绑定(bind)是:
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
函数有什么问题?如何制作正确的绑定(bind)适配器?
最佳答案
找不到访问器 com.example.feed.DummyViewModel.imageUrl
DummyViewModel don't have get method: getImageUrl()
你也可以定义公共(public):
public String imageUrl
关于android - 无法为 Android 数据绑定(bind)创建自定义 setter - "Could not find accessor"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32403915/