Android: Your content must have a TabHost whose id attribute is ‘android.R.id.tabhost’ 错误分析及解决方法 2014-10-19 / 10,738 次 / 快抢沙发 /

摘要:使用 TabActivity 的时候,TabHost 的 ID 声明必须为 android:id=”@android:id/tabhost” ,同时 TabHost 内部必须要有 ID 声明为 android:id=”@android:id/tabs” 的 控件 TabWidget 和 ID 声明为 android:id=”@android:id/tabcontent” 的布局。

Runtime Environment
OS: Windows 8.1
IDE: ADT Bundle v22.6.2
Device: Nexus 5 / Android 4.4.4
在调试 Android 工程的时候,出现错误:Your content must have a TabHost whose id attribute is ‘android.R.id.tabhost’ ,错误日志如下:
10-19 14:33:31.171: D/AndroidRuntime(19800): Shutting down VM
10-19 14:33:31.171: W/dalvikvm(19800): threadid=1: thread exiting with uncaught exception (group=0x415a3ba8)
10-19 14:33:31.201: E/AndroidRuntime(19800): FATAL EXCEPTION: main
10-19 14:33:31.201: E/AndroidRuntime(19800): Process: com.ifeegoo.debug, PID: 19800
10-19 14:33:31.201: E/AndroidRuntime(19800): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ifeegoo.debug/com.ifeegoo.debug.activities.MainActivity}: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
10-19 14:33:31.201: E/AndroidRuntime(19800): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
10-19 14:33:31.201: E/AndroidRuntime(19800): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
10-19 14:33:31.201: E/AndroidRuntime(19800): 	at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-19 14:33:31.201: E/AndroidRuntime(19800): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-19 14:33:31.201: E/AndroidRuntime(19800): 	at android.os.Handler.dispatchMessage(Handler.java:102)
10-19 14:33:31.201: E/AndroidRuntime(19800): 	at android.os.Looper.loop(Looper.java:136)
10-19 14:33:31.201: E/AndroidRuntime(19800): 	at android.app.ActivityThread.main(ActivityThread.java:5001)
10-19 14:33:31.201: E/AndroidRuntime(19800): 	at java.lang.reflect.Method.invokeNative(Native Method)
10-19 14:33:31.201: E/AndroidRuntime(19800): 	at java.lang.reflect.Method.invoke(Method.java:515)
10-19 14:33:31.201: E/AndroidRuntime(19800): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-19 14:33:31.201: E/AndroidRuntime(19800): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-19 14:33:31.201: E/AndroidRuntime(19800): 	at dalvik.system.NativeStart.main(Native Method)
10-19 14:33:31.201: E/AndroidRuntime(19800): Caused by: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
10-19 14:33:31.201: E/AndroidRuntime(19800): 	at android.app.TabActivity.onContentChanged(TabActivity.java:131)
10-19 14:33:31.201: E/AndroidRuntime(19800): 	at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:293)
10-19 14:33:31.201: E/AndroidRuntime(19800): 	at android.app.Activity.setContentView(Activity.java:1929)
10-19 14:33:31.201: E/AndroidRuntime(19800): 	at com.ifeegoo.debug.activities.MainActivity.onCreate(MainActivity.java:43)
10-19 14:33:31.201: E/AndroidRuntime(19800): 	at android.app.Activity.performCreate(Activity.java:5231)
10-19 14:33:31.201: E/AndroidRuntime(19800): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-19 14:33:31.201: E/AndroidRuntime(19800): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
10-19 14:33:31.201: E/AndroidRuntime(19800): 	... 11 more
10-19 14:33:42.611: I/Process(19800): Sending signal. PID: 19800 SIG: 9

本工程中用到了 TabActivity :

/**
 * 
 * @author ifeegoo www.ifeegoo.com
 *
 */
@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity
{
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_main);
	}
}

查到报错的源代码:

/**
 * 
 * @author ifeegoo www.ifeegoo.com
 *
 */
@Deprecated
public class TabActivity extends ActivityGroup 
{
    /**
     * Updates the screen state (current list and other views) when the
     * content changes.
     * 
     *@see Activity#onContentChanged()
     */
    @Override
    public void onContentChanged() {
        super.onContentChanged();
        mTabHost = (TabHost) findViewById(com.android.internal.R.id.tabhost);

        if (mTabHost == null) {
            throw new RuntimeException(
                    "Your content must have a TabHost whose id attribute is " +
                    "'android.R.id.tabhost'");
        }
        mTabHost.setup(getLocalActivityManager());
    }
}

从以上代码可以看出,你必须在 TabHost 控件中的 id 必须为 android.R.id.tabhost 。

我们在 Activity 的布局文件中,添加 TabHost 的 ID 声明为 android:id=”@android:id/tabhost” ,再次编译运行,问题得到解决!

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    ...

</TabHost>

同样,如果你在 TabHost 控件内部缺少ID 声明为 android:id=”@android:id/tabs” 的 控件 TabWidget 和 ID 声明为 android:id=”@android:id/tabcontent” 的布局,也会出现相关错误。

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/background_main"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </FrameLayout>

    </LinearLayout>

</TabHost>
E/AndroidRuntime(21775): Caused by: java.lang.RuntimeException: Your TabHost must have a FrameLayout whose id attribute is 'android.R.id.tabcontent'
E/AndroidRuntime(22104): Caused by: java.lang.RuntimeException: Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'
打赏
上一篇: « 下一篇: »
暂无相关文章
Copyright © ifeegoo Time is limited, less is more! / 粤ICP备15109713号 / Theme by Hang & Ben / WordPress / 知识共享许可协议