FragmentTabHost与活动意图崩溃

时间:2013-05-28 05:01:05

标签: android android-intent android-tabhost fragment-tab-host

我正在尝试创建一个包含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

我做错了什么?

1 个答案:

答案 0 :(得分: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/

希望这有帮助。