gpt4 book ai didi

Android 应用程序不断崩溃与 tabhost

转载 作者:行者123 更新时间:2023-11-30 03:04:22 26 4
gpt4 key购买 nike

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

这是日志:

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

list 文件:

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

}

请帮忙!

最佳答案

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

扩展 TabActivity

或者使用:

公共(public)无效设置(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.

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

关于Android 应用程序不断崩溃与 tabhost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22059519/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com