解决:android.content.res.Resources$NotFoundException: String resource ID #0x1

解决:android.content.res.Resources$NotFoundException: String resource ID #0x1

6551发表于2016-04-28

昨天晚上调试段android代码,被坑惨了,调到凌晨1点多,不过总算出来了。一直各种的报错,也怪刚接触android。下面分享一个问题,希望和别和我一样,踩着地雷。下面是LogCat输出的异常信息(这一点还是比较认可的,只要android闪退一般都为找到对应的异常堆栈信息,这个信息比较详细,能够精确到哪一行出错):

04-28 21:49:46.626: I/Timeline(25714): Timeline: Activity_idle id: android.os.BinderProxy@f5164df time:50336032
04-28 21:49:46.726: W/ResourceType(25714): No package identifier when getting value for resource number 0x0000000b
04-28 21:49:46.736: D/AndroidRuntime(25714): Shutting down VM
04-28 21:49:46.756: E/AndroidRuntime(25714): FATAL EXCEPTION: main
04-28 21:49:46.756: E/AndroidRuntime(25714): Process: com.seo.lanhusoft, PID: 25714
04-28 21:49:46.756: E/AndroidRuntime(25714): android.content.res.Resources$NotFoundException: String resource ID #0xb
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.content.res.Resources.getText(Resources.java:326)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.content.res.MiuiResources.getText(MiuiResources.java:108)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.widget.TextView.setText(TextView.java:4142)
04-28 21:49:46.756: E/AndroidRuntime(25714): at com.lanhusoft.adapter.KeywordsRankAdapter.getView(KeywordsRankAdapter.java:55)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:220)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.widget.AbsListView.obtainView(AbsListView.java:2347)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.widget.ListView.makeAndAddView(ListView.java:1864)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.widget.ListView.fillDown(ListView.java:698)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.widget.ListView.fillSpecific(ListView.java:1356)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.widget.ListView.layoutChildren(ListView.java:1651)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.widget.AbsListView.onLayout(AbsListView.java:2151)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.view.View.layout(View.java:15714)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.view.ViewGroup.layout(ViewGroup.java:5039)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.view.View.layout(View.java:15714)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.view.ViewGroup.layout(ViewGroup.java:5039)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.view.View.layout(View.java:15714)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.view.ViewGroup.layout(ViewGroup.java:5039)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1589)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.view.View.layout(View.java:15714)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.view.ViewGroup.layout(ViewGroup.java:5039)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.view.View.layout(View.java:15714)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.view.ViewGroup.layout(ViewGroup.java:5039)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.view.View.layout(View.java:15714)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.view.ViewGroup.layout(ViewGroup.java:5039)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.view.View.layout(View.java:15714)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.view.ViewGroup.layout(ViewGroup.java:5039)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.view.View.layout(View.java:15714)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.view.ViewGroup.layout(ViewGroup.java:5039)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.view.View.layout(View.java:15714)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.view.ViewGroup.layout(ViewGroup.java:5039)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2104)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1861)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1079)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5914)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.view.Choreographer.doCallbacks(Choreographer.java:580)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.view.Choreographer.doFrame(Choreographer.java:550)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.os.Handler.handleCallback(Handler.java:739)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.os.Handler.dispatchMessage(Handler.java:95)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.os.Looper.loop(Looper.java:135)
04-28 21:49:46.756: E/AndroidRuntime(25714): at android.app.ActivityThread.main(ActivityThread.java:5269)
04-28 21:49:46.756: E/AndroidRuntime(25714): at java.lang.reflect.Method.invoke(Native Method)
04-28 21:49:46.756: E/AndroidRuntime(25714): at java.lang.reflect.Method.invoke(Method.java:372)
04-28 21:49:46.756: E/AndroidRuntime(25714): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:910)
04-28 21:49:46.756: E/AndroidRuntime(25714): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:705)
04-28 21:49:46.816: I/Process(25714): Sending signal. PID: 25714 SIG: 9


可以从上面看出 FATAL EXCEPTION: main


android.content.res.Resources$NotFoundException: String resource ID #0xb

其中最有用的是下面一行:

at com.lanhusoft.adapter.KeywordsRankAdapter.getView(KeywordsRankAdapter.java:55)

KeywordsRankAdapter.java的55行,为TextView赋值报错了。

原因:

textView.setText函数,这种函数通常有几个重载,如:

textView.setText(CharSequence text);

textView.setText(int resId);

......
如果不小心将一个int值传给了它,那它不会显示该int值,而是跑到工程下去找一个对应的resource的id,当然是找不到的,于是就报错啦。

 
解决办法:

如果要显示该int值,就要将int转化成String或者CharSequence。

我一般是String.valueOf(x),x为int。





小编蓝狐