Android应用程序保持与tabhost崩溃 [英] Android app keeps crashing with tabhost

查看:221
本文介绍了Android应用程序保持与tabhost崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

下面是logcat的:

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)

下面是我用tabHost布局XML:

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>

我的code为主要的TabBar类:

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

清单文件:

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

终于为第一个选项卡类(第2个选项卡是pretty大同小异):

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

}

请帮帮忙!

推荐答案

<一个href=\"http://developer.android.com/reference/android/widget/TabHost.html#setup(android.app.LocalActivityManager)\" rel=\"nofollow\">http://developer.android.com/reference/android/widget/TabHost.html#setup(android.app.LocalActivityManager)

延长TabActivity

或使用:

公共无效设置(LocalActivityManager的ActivityGroup)

如果您使用的是setContent(android.content.Intent),因为是需要的ActivityGroup推出本地本活动必须调用。这是为你,如果你扩展TabActivity完成。

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.

参数
  的ActivityGroup用于发射活动标签的内容。

Parameters activityGroup Used to launch activities for tab content.

即。 mTab​​Host.setup(activityManager)

别人同样的问题:

<一个href=\"http://stackoverflow.com/questions/9409735/java-lang-illegalstateexception-at-tabhost-addtabspec\">java.lang.IllegalStateException.....在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);

这篇关于Android应用程序保持与tabhost崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆