I'm trying to make a main menu that contains a FragmentTabHost
which contains two Intents
to open up some Activities
but it keeps crashing when I try running it.
The 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>
And the code for 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);
}
}
And the error log
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
What am I doing wrong?
You've probably moved on from this but I had the same issue.
Make sure that you are using Fragments instead of Activities for the tab contents.
When creating the tabs: Instead of:
inCall = new Intent("com.example.callfinder.FINDCALLS");
TabSpec specCall = tabHost.newTabSpec("calls");
specCall.setIndicator("Calls").setContent(inCall);
tabHost.addTab(specCall);
replace with:
TabSpec specCall = tabHost.newTabSpec("calls").setIndicator("Calls");
tabHost.addTab(specCall, <ClassName>.class, null);
These links are what helped me piece it all together: http://developer.android.com/reference/android/support/v4/app/FragmentTabHost.html http://apptechinfo.com/android-fragment-tab-example/
Hope this helps.