当前位置:主页 > 移动开发 > Android代码 >

Android为textView设置setText的时候报错的讲解方案

时间:2021-02-16 10:41:23 | 栏目:Android代码 | 点击:

在对中TextView setText 覆值int 时报错,网上查下原因是setText整型表明是设值R.id.xxx,当然找不到。

解决方法是将int转化为string,用String.valueOf(xxx)

一、我的代码如下:就是我textView设置值

if (list != null) {
  for (Student stu : list) {
    //如果一下子赋值的话是不正确的
    tv_name.setText(stu.getName());
    tv_sex.setText(stu.getSex());
    tv_age.setText(stu.getAge());
  }
}

报出错误:

04-04 08:56:33.950: W/ResourceType(23332): No package identifier when getting value for resource number 0x0000000c
04-04 08:56:33.950: D/AndroidRuntime(23332): Shutting down VM
04-04 08:56:33.950: W/dalvikvm(23332): threadid=1: thread exiting with uncaught exception (group=0x416ddd58)
04-04 08:56:33.960: E/AndroidRuntime(23332): FATAL EXCEPTION: main
04-04 08:56:33.960: E/AndroidRuntime(23332): Process: com.xuliugen.studentsytem, PID: 23332
04-04 08:56:33.960: E/AndroidRuntime(23332): android.content.res.Resources$NotFoundException: String resource ID #0xc
android.content.res.Resources.getText(Resources.java:252)
04-04 08:56:33.960: E/AndroidRuntime(23332):    at android.content.res.MiuiResources.getText(MiuiResources.java:107)
04-04 08:56:33.960: E/AndroidRuntime(23332):    at android.widget.TextView.setText(TextView.java:3895)
04-04 08:56:33.960: E/AndroidRuntime(23332):    at com.xuliugen.ui.activity.StudentManagerActivity$1.onClick(StudentManagerActivity.java:82)
04-04 08:56:33.960: E/AndroidRuntime(23332):    at android.view.View.performClick(View.java:4444)
04-04 08:56:33.960: E/AndroidRuntime(23332):    at android.view.View$PerformClick.run(View.java:18457)
04-04 08:56:33.960: E/AndroidRuntime(23332):    at android.os.Handler.handleCallback(Handler.java:733)
04-04 08:56:33.960: E/AndroidRuntime(23332):    at android.os.Handler.dispatchMessage(Handler.java:95)
04-04 08:56:33.960: E/AndroidRuntime(23332):    at android.os.Looper.loop(Looper.java:136)
04-04 08:56:33.960: E/AndroidRuntime(23332):    at android.app.ActivityThread.main(ActivityThread.java:5034)
04-04 08:56:33.960: E/AndroidRuntime(23332):    at java.lang.reflect.Method.invokeNative(Native Method)
04-04 08:56:33.960: E/AndroidRuntime(23332):    at java.lang.reflect.Method.invoke(Method.java:515)
04-04 08:56:33.960: E/AndroidRuntime(23332):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:807)
04-04 08:56:33.960: E/AndroidRuntime(23332):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:623)
04-04 08:56:33.960: E/AndroidRuntime(23332):    at dalvik.system.NativeStart.main(Native Method)

具体解决方法:

if (list != null) {
  for (Student stu : list) {
    //如果一下子赋值的话是不正确的
    tv_name.setText(stu.getName()+"");
    tv_sex.setText(stu.getSex()+"");
    tv_age.setText(stu.getAge()+"");            
  }
}

总结

您可能感兴趣的文章:

相关文章