Android应用程序与tabhost一起崩溃

时间:2014-02-27 05:10:59

标签: android android-layout android-tabhost crash

我的目标是创建两个标签,每个标签都有不同的活动。我希望标签始终在顶部可见,这样我就可以轻松地在两者之间切换。我相信一切都设置为使用tabHost创建两个选项卡,但是当我运行它时,应用程序在启动时崩溃。

这是logcat:

Process: com.example.app, PID: 1321
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.TabBar}: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
            at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:747)
            at android.widget.TabHost.setCurrentTab(TabHost.java:413)
            at android.widget.TabHost.addTab(TabHost.java:240)
            at com.example.app.TabBar.onCreate(TabBar.java:31)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

这是我使用tabHost:

的布局的xml
<?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"
    android:orientation="vertical" >

    <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" >
        </TabWidget>

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

            <RelativeLayout
                android:id="@+id/calc"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/tip"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >
            </RelativeLayout>
        </FrameLayout>
    </LinearLayout>
</TabHost>

我的主要tabBar类的代码:

public class TabLayout extends Activity {

    private TabHost mTabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_layout);
        mTabHost = getTabHost();

        Intent intent;
        //Creates calculator tab
        intent = new Intent(this, Calculator.class);
        TabHost.TabSpec spec1 = mTabHost.newTabSpec("calc")
                .setIndicator("CALC")
                .setContent(intent);
        mTabHost.addTab(spec1);

        //Creates tip tab
        intent = new Intent(this, TipCalc.class);
        TabHost.TabSpec spec2 = mTabHost.newTabSpec("tip")
                .setIndicator("TIP")
                .setContent(intent);
        mTabHost.addTab(spec2);
    }
}

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.calcs">

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/calculators"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.calcs.TabLayout"
            android:label="@string/calculators" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <activity android:name="com.example.calcs.Calculator"/>
        <activity android:name="com.example.calcs.TipCalc"/>

    </application>

</manifest>

最后是第一个标签的类(第二个标签几乎相同):

public class Calculator extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_layout);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.tab_layout, menu);
        return true;
    }

}

请帮忙!

1 个答案:

答案 0 :(得分:1)

http://developer.android.com/reference/android/widget/TabHost.html#setup(android.app.LocalActivityManager)

extend TabActivity

或使用:

public void setup (LocalActivityManager activityGroup)

  

如果您正在使用setContent(android.content.Intent),则必须调用此方法,因为启动本地活动需要activityGroup。如果您扩展TabActivity,则会为您完成此操作。

     

参数   activityGroup用于启动选项卡内容的活动。

即。 mTabHost.setup(activityManager)


其他人也有同样的问题:

java.lang.IllegalStateException..... at tabhost.add(tabspec);

LocalActivityManager mLocalActivityManager = new LocalActivityManager(mActivity, false);
mLocalActivityManager.dispatchCreate(state); // state will be bundle your activity state which you get in onCreate
mTabHost.setup(mLocalActivityManager);