Android与整数类型的两种方式绑定导致数据绑定不存在 [英] Android two way binding with Integer type causes databinding does not exist
问题描述
在使用Integer数据类型实现双向绑定时遇到了一些问题.
I'm having some issue with implementing two way binding with an Integer data type.
public class User {
private String firstName;
private String lastName;
private int age;
public User() {}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return this.firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getLastName() {
return this.lastName;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return this.age;
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data class="UserDataBinding">
<variable
name="user"
type="com.databinding.model.User" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="@dimen/activity_horizontal_margin">
<EditText android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@={user.firstName}" />
<EditText android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@={user.lastName}" />
<EditText android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@={user.age}" />
</LinearLayout>
</layout>
不幸的是,它给了我错误
Unfortunately, it gives me the error
错误:(52,17)找不到属性'android:text'的吸气剂 值类型为java.lang.Integer的对象 android.support.design.widget.TextInputEditText. "
"Error:(52, 17) Cannot find the getter for attribute 'android:text' with value type java.lang.Integer on android.support.design.widget.TextInputEditText. "
如果我将属性文本更改为
If I change the attribute text to
<EditText android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@={Integer.toString(user.age)}" />
然后我得到错误
错误:无法生成视图联编程序java.lang.NullPointerException"
"Error:cannot generate view binders java.lang.NullPointerException"
对此表示感谢.
更新:上述错误之后似乎还有另一个错误.
UPDATE: It seems there was another error right after the error mentioned above.
无法生成视图绑定程序java.lang.NullPointerException
cannot generate view binders java.lang.NullPointerException
即使应用程序尚未启动,也不确定为什么它会给我NPE.
Not sure why its giving me NPE even though the app hasn't started yet.
推荐答案
嗯,六个月后,但也许我可以帮助某人.
Well, six months later but maybe i can help someone.
您可以执行以下简单操作:
You can do this simple trick:
android:text="@={`` + mObject.someNumber}"
OBS .:您至少需要 Android Studio 2.3
这篇关于Android与整数类型的两种方式绑定导致数据绑定不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!