今天在测试时候,老是有一个TextView报错,以下是错误日志:
java.lang.RuntimeException:
Unable to resume activity {com....Activity}: android.content.res.Resources$NotFoundException: String resource ID #0x0
…
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x0
…
at com.gidoor.runner.ui.main.OrderManagerActivity.setOrderCountValue(...Activity.java:133)
定位到相关代码:
罪魁祸首就是reservedCount这个变量(int类型)。
再来看看TextView.setText的方法:
final void
setText(int resid)
final void
setText(char[] text, int start, int len)Sets the TextView to display the specified slice of the specified char array.
final void
setText(int resid, TextView.BufferType type)
final void
setText(CharSequence text)Sets the string value of the TextView.
void
setText(CharSequence text, TextView.BufferType type)Sets the text that this TextView is to display (see setText(CharSequence)) and also sets whether it is stored in a styleable/spannable buffer and whether it is editable.
看到这里,就知道了,原来这执行这行代码的时候,系统去R.Java里面寻找ID为0(上面的reservedCount值为0)的String,没有找到。
到此,终于排雷成功。It’s funny!