Android 异常:Resources NotFoundException

2013-08-19

Android 异常:Resources NotFoundException

2013-08-19

异常代码片段

int index = 0;
textview.setText(index);

异常信息

//报错信息略
java.lang.RuntimeException:
Unable to start activity ComponentInfo{
  com.example.errors/com.example.errors.MainActivity//发生异常的类
}:
android.content.res.Resources$NotFoundException:String resource ID #0x0   

异常原因

TextView有个setText(int resid)方法,但里面的int值是资源id,在R文件里有记录的,例如R.string.xx,当你随意填入一个int数,而资源文件里又没有该id时,就会报Resources$NotFoundException;

异常解决

将传入的int类型转换成String 类型

int index = 0;
textview.setText(String.valueOf(index));