0

我正在尝试制作一个包含 a 的主菜单,FragmentTabHost其中包含两个Intents以打开一些Activities但是当我尝试运行它时它一直在崩溃。

XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

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

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

    <FrameLayout
        android:id="@+id/realtabcontent"
        android:layout_width="match_parent"
        android:layout_height="0dp" />
</RelativeLayout>

以及 MainMenu 的代码

package com.example.callfinder;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.widget.TabHost.TabSpec;


public class MainMenu extends FragmentActivity  {

private FragmentTabHost tabHost;
Intent inCall, inText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_menu);

    tabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
    tabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

    inCall = new Intent("com.example.callfinder.FINDCALLS");
    TabSpec specCall = tabHost.newTabSpec("calls");
    specCall.setIndicator("Calls").setContent(inCall);
    tabHost.addTab(specCall);



    inText = new Intent("com.example.callfinder.FINDTEXTS");
    TabSpec specText = tabHost.newTabSpec("text");
    specText.setIndicator("Texts").setContent(inText);
    tabHost.addTab(specText);

    tabHost.setCurrentTab(0);

}


}

和错误日志

05-28 00:55:08.634: E/AndroidRuntime(28026): FATAL EXCEPTION: main
05-28 00:55:08.634: E/AndroidRuntime(28026): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.callfinder/com.example.callfinder.MainMenu}: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
05-28 00:55:08.634: E/AndroidRuntime(28026):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
05-28 00:55:08.634: E/AndroidRuntime(28026):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2356)
05-28 00:55:08.634: E/AndroidRuntime(28026):    at android.app.ActivityThread.access$600(ActivityThread.java:150)
05-28 00:55:08.634: E/AndroidRuntime(28026):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
05-28 00:55:08.634: E/AndroidRuntime(28026):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-28 00:55:08.634: E/AndroidRuntime(28026):    at android.os.Looper.loop(Looper.java:137)
05-28 00:55:08.634: E/AndroidRuntime(28026):    at android.app.ActivityThread.main(ActivityThread.java:5195)
05-28 00:55:08.634: E/AndroidRuntime(28026):    at java.lang.reflect.Method.invokeNative(Native Method)
05-28 00:55:08.634: E/AndroidRuntime(28026):    at java.lang.reflect.Method.invoke(Method.java:511)
05-28 00:55:08.634: E/AndroidRuntime(28026):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
05-28 00:55:08.634: E/AndroidRuntime(28026):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
05-28 00:55:08.634: E/AndroidRuntime(28026):    at dalvik.system.NativeStart.main(Native Method)
05-28 00:55:08.634: E/AndroidRuntime(28026): Caused by: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
05-28 00:55:08.634: E/AndroidRuntime(28026):    at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:747)
05-28 00:55:08.634: E/AndroidRuntime(28026):    at android.widget.TabHost.setCurrentTab(TabHost.java:413)
05-28 00:55:08.634: E/AndroidRuntime(28026):    at android.widget.TabHost.addTab(TabHost.java:240)
05-28 00:55:08.634: E/AndroidRuntime(28026):    at com.example.callfinder.MainMenu.onCreate(MainMenu.java:27)
05-28 00:55:08.634: E/AndroidRuntime(28026):    at android.app.Activity.performCreate(Activity.java:5104)
05-28 00:55:08.634: E/AndroidRuntime(28026):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-28 00:55:08.634: E/AndroidRuntime(28026):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2260)
05-28 00:55:08.634: E/AndroidRuntime(28026):    ... 11 more

我究竟做错了什么?

4

1 回答 1

1

你可能已经离开了,但我遇到了同样的问题。

确保您使用片段而不是活动作为选项卡内容。

创建选项卡时:而不是:

inCall = new Intent("com.example.callfinder.FINDCALLS");
TabSpec specCall = tabHost.newTabSpec("calls");
specCall.setIndicator("Calls").setContent(inCall);
tabHost.addTab(specCall);

用。。。来代替:

TabSpec specCall = tabHost.newTabSpec("calls").setIndicator("Calls");
tabHost.addTab(specCall, <ClassName>.class, null);

这些链接帮助我将它们拼凑在一起:http: //developer.android.com/reference/android/support/v4/app/FragmentTabHost.html http://apptechinfo.com/android-fragment-tab-example/

希望这可以帮助。

于 2014-02-25T06:30:29.873 回答