【已解决】Android中用TabHost去addTab添加TAB结果出错:Unable to start activity ComponentInfo: android.content.res.Resources$NotFoundException: Resource ID #0x0

【问题】

折腾:

【未解决】Android中的TabHost中的TAB中的FrameLayout中的View显示位置异常出现重叠且覆盖了TAB标签

期间,执行代码:

		 // construct the TAB Host
		TabHost tabHost = new TabHost(this);
		tabHost.setLayoutParams(
				new LinearLayout.LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		 
		......

		tabHost.setup();		
		......
	    TabSpec ts1 = tabHost.newTabSpec("Tab1");
	    ts1.setIndicator("Tab 1");
	    //spec1.setContent(R.id.tab1);
	    ts1.setContent(new TabHost.TabContentFactory() {
	        ......
	    });
	    tabHost.addTab(ts1);

时出错:

android tabHost addTab crash logcat detail

 

log为:

03-07 15:14:54.973: E/AndroidRuntime(3743): FATAL EXCEPTION: main
03-07 15:14:54.973: E/AndroidRuntime(3743): java.lang.RuntimeException: Unable to start activity ComponentInfo{xxx/xxx.UI.MainActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x0
03-07 15:14:54.973: E/AndroidRuntime(3743): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
03-07 15:14:54.973: E/AndroidRuntime(3743): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
03-07 15:14:54.973: E/AndroidRuntime(3743): 	at android.app.ActivityThread.access$700(ActivityThread.java:150)
03-07 15:14:54.973: E/AndroidRuntime(3743): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
03-07 15:14:54.973: E/AndroidRuntime(3743): 	at android.os.Handler.dispatchMessage(Handler.java:99)
03-07 15:14:54.973: E/AndroidRuntime(3743): 	at android.os.Looper.loop(Looper.java:176)
03-07 15:14:54.973: E/AndroidRuntime(3743): 	at android.app.ActivityThread.main(ActivityThread.java:5279)
03-07 15:14:54.973: E/AndroidRuntime(3743): 	at java.lang.reflect.Method.invokeNative(Native Method)
03-07 15:14:54.973: E/AndroidRuntime(3743): 	at java.lang.reflect.Method.invoke(Method.java:511)
03-07 15:14:54.973: E/AndroidRuntime(3743): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
03-07 15:14:54.973: E/AndroidRuntime(3743): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
03-07 15:14:54.973: E/AndroidRuntime(3743): 	at dalvik.system.NativeStart.main(Native Method)
03-07 15:14:54.973: E/AndroidRuntime(3743): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
03-07 15:14:54.973: E/AndroidRuntime(3743): 	at android.content.res.Resources.getValue(Resources.java:1883)
03-07 15:14:54.973: E/AndroidRuntime(3743): 	at android.content.res.Resources.loadXmlResourceParser(Resources.java:3028)
03-07 15:14:54.973: E/AndroidRuntime(3743): 	at android.content.res.Resources.getLayout(Resources.java:1722)
03-07 15:14:54.973: E/AndroidRuntime(3743): 	at android.view.LayoutInflater.inflate(LayoutInflater.java:395)
03-07 15:14:54.973: E/AndroidRuntime(3743): 	at android.widget.TabHost$LabelIndicatorStrategy.createIndicatorView(TabHost.java:615)
03-07 15:14:54.973: E/AndroidRuntime(3743): 	at android.widget.TabHost.addTab(TabHost.java:244)
03-07 15:14:54.973: E/AndroidRuntime(3743): 	at xxx.UI.MainActivity.onCreate(MainActivity.java:211)
03-07 15:14:54.973: E/AndroidRuntime(3743): 	at android.app.Activity.performCreate(Activity.java:5267)
03-07 15:14:54.973: E/AndroidRuntime(3743): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
03-07 15:14:54.973: E/AndroidRuntime(3743): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
03-07 15:14:54.973: E/AndroidRuntime(3743): 	... 11 more

 

【解决过程】

1.看起来,像是缺少资源而出错。

但是问题有点奇怪的是:

此处的代码是参考:

Android dynamic TAB Control « PocketMagic

中的,而那个帖子中,貌似是可以正常新建TabHost,用addTab去添加Tab的啊。

2.另外,之前参考别的帖子,去新建的xml,而后用:

final TabHost tabHost = (TabHost)findViewById(R.id.tabHost);
tabHost.setup();

时,后续的addTab就不会出错,就可以正常运行的。

所以,看起来此处是不能自己创建TabHost,而必须要用xml中的TabHost?

3.参考:

tabHost.addTab() in android – Stack Overflow

没用。

4.参考:

android – java.lang.RuntimeException: Unable to start activity ComponentInfo – Stack Overflow

有人说那人是缺少activity,而我此处是正常加了对应activity的:

        <activity
            android:name="xxx.xxx.xxx.UI.MainActivity"
            android:label="@string/edit_mode_view_title" >
        </activity>

但是后来楼主自己都找到错误原因了:是自己不小心写错了,把:

java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());

放到onCreate中就好了。我此处貌似没有这样的问题。

5.参考:

(Android) Unable to start activity ComponentInfo – Stack Overflow

也没用。

6.这个:

Android – Tabhost working in Activity class – Stack Overflow

貌似解释的不错。

不过我此处的错误是:

android.content.res.Resources$NotFoundException: Resource ID #0x0

和那人的:

java.lang.IllegalStateException

不太一样。

7.再去搜:

addTab Unable to start activity ComponentInfo NotFoundException: Resource ID #0x0

参考:

resources – ResourceNotFoundException Thrown From TabHost.addTab() – Stack Overflow

->

android – Honeycomb and TabHost specs – Stack Overflow

说是改为:

TabHost myTabHost = new TabHost(getContext(), null); 

就可以了。

待会去试试。

因为此处看到另外回复的人又说道:

其也是把上述动态创建和配置TabHost等内容,放到xml中,也就解决了问题了:

这点和我之前所遇到的是一样的。。。

所以有点怀疑:不会是Android的bug吧。。。

先不管,去试试:

		//TabHost tabHost = new TabHost(this);
		TabHost tabHost = new TabHost(getContext(), null);

的确是不行:

【已解决】Android中找不到getContext:The method getContext() is undefined for the type

 

【总结】

然后上述代码后续调试通过,最终是用:

		//TabHost tabHost = new TabHost(getContext(), null);
		TabHost tabHost = new TabHost(getBaseContext(), null);

即可解决后续addTab出错的问题。



发表评论

电子邮件地址不会被公开。 必填项已用*标注

无觅相关文章插件,快速提升流量