Android app keeps crashing with tabhost

冷暖自知 提交于 2019-12-08 07:26:46

问题


My goal is to have two tabs created, each with a different activity. I want the tabs to always be visible at the top so I can easily switch between the two. I believe to have everything set up to create two tabs using tabHost, however the app crashes on start up when I run it.

Here is the 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)

Here is my xml for the layout with the 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"
    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>

My code for the main tabBar class:

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);
    }
}

The manifest file:

<?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>

and finally the class for the first tab (2nd tab is pretty much the same):

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;
    }

}

Please help!


回答1:


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

Either extend TabActivity

Or use:

public void setup (LocalActivityManager activityGroup)

If you are using setContent(android.content.Intent), this must be called since the activityGroup is needed to launch the local activity. This is done for you if you extend TabActivity.

Parameters activityGroup Used to launch activities for tab content.

i.e. mTabHost.setup(activityManager)


Someone else same problem:

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);


来源:https://stackoverflow.com/questions/22059519/android-app-keeps-crashing-with-tabhost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!