android - 无法为 Android 数据绑定(bind)创建自定义 setter - "Could not find accessor"错误

标签 android android-databinding

我正在尝试按照此处所述创建自定义 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/

相关文章:

java - 如何拦截 Android 耳机上的按钮按下?

android - 谷歌云消息 (GCM) 问题

android - 找不到 ActivityMainBindingImpl

android - 更新到 Android Studio 2.3 后无法构建

Android DataBinding 构建错误

c# - Xamarin:Android - 大位图的 OutOfMemory 异常 - 如何解决?

android - 在 android 中如何在 ArrayList 中存储 EditText 值

Android:在捕获或从图库中选取后裁剪图像

Android 仪器测试 dexing 错误

在自定义 View 布局中使用包含标记时 Android 数据绑定(bind)崩溃