Android 数据绑定(bind)错误。找不到访问器

标签 android data-binding android-databinding

当我尝试运行我的应用程序时出现以下错误:

Error:Execution failed for task ':app:compileDevelopmentDebugJavaWithJavac'.
> java.lang.RuntimeException: Found data binding errors.
****/ data binding error ****msg:Could not find accessor java.lang.String.giftRecipientName redacted.xml loc:182:63 - 182:93 ****\ data binding error ****

我有一个 Order 对象,如下所示:

public class Order {
    public Address address;
    // unrelated fields and methods
}

嵌套的 Address 对象如下所示:

public class Address {
    public String addressLine1;
    public String addressLine2;
    public String giftRecipientName;
    public Boolean isGift;
}

在我的 .xml 中,我正在执行以下操作:

<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        <variable name="order" type="example.redacted.models.Order"/>
    </data>
    // widgets and whatnot
    <TextView
        android:id="@+id/gift_recipientTV"
        android:layout_column="1"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:textStyle="bold"
        android:gravity="right"
        android:text='@{order.address.isGift ?  order.address.giftRecipientName : "" }'/>

最后在我的 fragment 中:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    RedactedBinding dataBinding = DataBindingUtil.inflate(inflater, R.layout.redacted, container, false);
    dataBinding.setOrder(_order);
    return dataBinding.getRoot();
}

最佳答案

经过数小时的反复试验,Android 数据绑定(bind)似乎会在 寻找公共(public)字段之前寻找 getter。我的 Order 对象有一个名为 getAddress 的辅助方法

public class Order {
    public Address address;

    public String getAddress() {
        return address.addressLine1 + address.addressLine2;
    }
}

Binder 正在调用该方法,而不是访问公共(public)地址字段。我将 getAddress 方法放在 Address 对象中(它可能应该从这里开始)并编译了应用程序。

关于Android 数据绑定(bind)错误。找不到访问器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32683129/

相关文章:

仪表板上未报告 Android Flurry 事件

android - 为具有不同项目布局的 ListView 创建 ViewHolders

.net - WPF 中的双向数据绑定(bind)到底是什么?

c# - WPF 数据绑定(bind)堆栈面板

android - 从数据绑定(bind) Android 调用 Kotlin 函数类型

android - 限制 Firebase 数据库访问一个 Android 应用程序

java - 如何使用左侧顶部的操作栏夏洛克图标进行操作?

java - android 数据绑定(bind) - 找不到类 android.view.data

android - 缺少双向数据绑定(bind)的 DatePicker 日、月和年属性?

android - 将通用列表传递到 <include>