Android DataBinding错误.找不到访问器 [英] Android DataBinding error. Could not find accessor
问题描述
尝试运行我的应用时出现以下错误:
I'm getting the following error when I try to run my app:
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对象:
I have an Order object which looks like this:
public class Order {
public Address address;
// unrelated fields and methods
}
嵌套的Address对象如下所示:
The nested Address object looks like this:
public class Address {
public String addressLine1;
public String addressLine2;
public String giftRecipientName;
public Boolean isGift;
}
在我的.xml文件中,我正在执行以下操作:
In my .xml I am doing the following:
<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 : "" }'/>
最后是我的片段:
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数据绑定似乎在之前查找吸气剂,而在公共字段中查找.我的Order对象有一个名为getAddress的帮助器方法
After hours of trial and error it seems that Android data-binding looks for getters before it looks at public fields. My Order object had a helper method called getAddress
public class Order {
public Address address;
public String getAddress() {
return address.addressLine1 + address.addressLine2;
}
}
活页夹正在调用该方法,而不是访问公共地址字段.我将getAddress方法放在Address对象(可能应该从那里开始)中,然后编译应用程序.
The binder was calling that method instead of accessing the public Address field. I put the getAddress method inside the Address object (where it probably should have been to begin with) and the app compiled.
这篇关于Android DataBinding错误.找不到访问器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!