数据绑定:当对象的属性为int时,Resources $ NotFoundException [英] Data Binding : Resources$NotFoundException when attribute of object is int
本文介绍了数据绑定:当对象的属性为int时,Resources $ NotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用数据绑定.如果我使用具有字符串属性的对象,则它可以正常工作,但是在这种情况下,我使用int,它不起作用. 我有对象User:
I am trying to use Data binding. It is work properly if I use object that has attribute of string, but in this case I use int and it doesn't work. I have object User:
public class User extends BaseObservable{
public int age;
......
public User() {}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
...
}
这是我的布局
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="user" type="com.example.bindingview.User"/>
</data>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{user.age}"/>
</LinearLayout>
</layout>
问题在于TextView不能包含年龄为int的文本. 如果我从int更改为age属性,它可以正常工作. 我应该怎么做才能避免这个问题?
The problem is that TextView cannot have text of age that is int. if I change from int to string of age attribute it worked fine. What should I do to avoid this problem?
推荐答案
只需添加String.valueOf():
Just add String.valueOf():
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{String.valueOf(user.age)}"/>
这篇关于数据绑定:当对象的属性为int时,Resources $ NotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文