我正在使用新的数据绑定库并尝试实现可观察的接口。但无论我是否将年龄变量设置为textview,它都会一直崩溃。如果我将age变量转换为String,代码工作正常。有没有办法让它适用于int?
public class User extends BaseObservable {
@Bindable
public final ObservableField<String> name =
new ObservableField<>();
@Bindable
public final ObservableInt age = new ObservableInt();}
崩溃日志:
android.content.res.Resources$NotFoundException: String resource ID #0x3
at android.content.res.Resources.getText(Resources.java:274)
at android.widget.TextView.setText(TextView.java:4122)
at com.hipo.databinding.databinding.ActivityMainBinding.executeBindings(ActivityMainBinding.java:184)
at android.databinding.ViewDataBinding.executePendingBindings(ViewDataBinding.java:321)
at android.databinding.ViewDataBinding$6.run(ViewDataBinding.java:160)
at android.databinding.ViewDataBinding$5.onViewAttachedToWindow(ViewDataBinding.java:130)
at android.view.View.dispatchAttachedToWindow(View.java:13417)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2707)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2714)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2714)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2714)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1292)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)public class User extends BaseObservable {
@Bindable
public final ObservableField<String> name =
new ObservableField<>();
@Bindable
public final ObservableInt age = new ObservableInt();
}
答案 0 :(得分:8)
将其放入数据标签
您需要在TextView中使用@ {String.valueOf(user.age)}。
或@ {&#34;&#34; + user.age}