In my app I've to navigate from the login activity (normal activity developed with a RelativeLayout) to an activity in which there are a Tab Host. I created the following xml for Tab Host:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linearLayout1"
android:orientation="vertical">
<TabWidget
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@android:id/tabs"
android:layout_marginBottom="-4dp">
</TabWidget>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/tabcontent"
android:layout_weight="1">
</FrameLayout>
</LinearLayout>
</TabHost>
This is the code of the class that control the Tab Host:
public class TabBarActivity extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabbar);
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, HomeActivity.class);
spec = tabHost.newTabSpec("Home").setIndicator("", getResources().getDrawable(R.drawable.home)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, HistoryActivity.class);
spec = tabHost.newTabSpec("History").setIndicator("", getResources().getDrawable(R.drawable.bell)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, SettingsActivity.class);
spec = tabHost.newTabSpec("Settings").setIndicator("", getResources().getDrawable(R.drawable.gear)).setContent(intent);
tabHost.addTab(spec);
}
}
My login activity send a username and a password to a web service and if the web service allow the user to login it should show the Tab Host with 3 different activity on the bottom. How I can show the activity with Tab Host? I tried to do so:
Intent i = new Intent(MainActivity.this, TabBarActivity.class);
startActivity(i);
But it crash. Can you help me?
UPDATE
02-26 15:16:27.533 5504-5504/com.sample.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.sample.app, PID: 5504
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sample.app/com.sample.app.TabBarActivity}: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
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:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
at android.app.TabActivity.onContentChanged(TabActivity.java:131)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:362)
at android.app.Activity.setContentView(Activity.java:2010)
at armandotesta.it.atbrain.TabBarActivity.onCreate(TabBarActivity.java:15)
at android.app.Activity.performCreate(Activity.java:5426)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
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:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Replace
TabHost tabHost = getTabHost();
with
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
tabHost.setup();