java.lang.RuntimeException:您的内容必须有一个TabHost,其id属性为' android.R.id.tabhost'

时间:2014-06-25 01:10:41

标签: android android-tabhost

我已经尝试了一切来尝试让它发挥作用。我想要一个标签窗口;我按照教程确保每一步都是正确的,但每次我将它加载到AVD上时,都会强行关闭。我是业余爱好者,所以对我很轻松!任何和所有帮助将不胜感激。

logcat的:

06-24 20:49:29.564    1146-1146/com.michail.vector E/Trace﹕ error opening trace file: No such file or directory (2)
06-24 20:49:31.997    1146-1146/com.michail.vector E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.michail.vector/com.michail.vector.MainActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.michail.vector/com.michail.vector.SimpleVector}: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
            at android.app.ActivityThread.access$600(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.michail.vector/com.michail.vector.SimpleVector}: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
            at android.app.ActivityThread.startActivityNow(ActivityThread.java:1900)
            at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
            at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
            at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:694)
            at android.widget.TabHost.setCurrentTab(TabHost.java:358)
            at android.widget.TabHost.addTab(TabHost.java:236)
            at com.michail.vector.MainActivity.onCreate(MainActivity.java:40)
            at android.app.Activity.performCreate(Activity.java:5008)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
            at android.app.ActivityThread.access$600(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
            at android.app.TabActivity.onContentChanged(TabActivity.java:131)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:259)
            at android.app.Activity.setContentView(Activity.java:1867)
            at com.michail.vector.SimpleVector.onCreate(SimpleVector.java:10)
            at android.app.Activity.performCreate(Activity.java:5008)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
            at android.app.ActivityThread.startActivityNow(ActivityThread.java:1900)
            at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
            at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
            at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:694)
            at android.widget.TabHost.setCurrentTab(TabHost.java:358)
            at android.widget.TabHost.addTab(TabHost.java:236)
            at com.michail.vector.MainActivity.onCreate(MainActivity.java:40)
            at android.app.Activity.performCreate(Activity.java:5008)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
            at android.app.ActivityThread.access$600(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
06-24 20:49:40.744      148-196/system_process E/PowerManagerService﹕ Excessive delay setting brightness: 193ms, mask=2
06-24 20:49:40.883      148-196/system_process E/PowerManagerService﹕ Excessive delay setting brightness: 141ms, mask=2
06-24 20:55:06.194      148-254/system_process E/ThrottleService﹕ problem during onPollAlarm: java.lang.IllegalStateException: problem parsing stats: java.io.FileNotFoundException: /proc/net/xt_qtaguid/iface_stat_all: open failed: ENOENT (No such file or directory)

MainActivity.java:

package com.michail.vector;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class MainActivity extends TabActivity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Create a new tabHost
        TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);


        /** SINGLE VECTOR TAB **/
        //Used to keep track of tab
        TabSpec single = tabHost.newTabSpec("Single Vector");

        //Change tab indicator name and icon if necessary
        single.setIndicator("Single Vector");


        //Intent: Description of an operation to be performed
        Intent singleIntent = new Intent(this, SimpleVector.class);

        //Uses intent to launch activity as the tab content
        single.setContent(singleIntent);

        /** TWO VECTOR TAB**/
        TabSpec two = tabHost.newTabSpec("Two Vectors");
        two.setIndicator("Two Vectors");
        Intent TwoIntent = new Intent(this, TwoVectors.class);
        two.setContent(TwoIntent);

        //Add the tabs
        tabHost.addTab(single);
        tabHost.addTab(two);

    }

}

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>

<TabHost
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@android:id/tabhost"
        xmlns:android="http://schemas.android.com/apk/res/android">


        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical">

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

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

                <LinearLayout
                    android:id="@+id/tab1"
                    android:orientation="vertical"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"/>

                <LinearLayout
                    android:id="@+id/tab2"
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"/>

            </FrameLayout>
        </LinearLayout>
    </TabHost>

的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.michail.vector"
    android:versionCode="1"
    android:versionName="1.0">

    <application
        android:icon="@drawable/ic_launcher"
        android:label="Vectors"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="Vectors" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".SimpleVector" />
        <activity android:name=".TwoVectors" />

    </application>

</manifest>

如果我需要提供任何其他类关联或xml文件,请询问。我包括了我认为回答这个问题所必需的内容。提前谢谢。

编辑:我决定使用带按钮的主屏幕,而不是制表符。这是一个简单的设计,我知道它会起作用

2 个答案:

答案 0 :(得分:1)

扩展TabActivity应如下所示:

TabHost mTabHost = getTabHost();

请注意,tabhost已弃用,建议将TabLayout与片段一起使用。

答案 1 :(得分:0)

嘿,我刚才遇到了和你一样的问题,但我不得不将我的MainActivity从TabActivity扩展到Activity并清理我的项目。每件事都运行得很好。试试吧