Android - 从主要活动导航到平板电脑活动

时间:2015-02-26 14:25:20

标签: android android-intent android-tabhost

在我的应用程序中,我要从登录活动(使用RelativeLayout开发的正常活动)导航到有Tab主机的活动。我为Tab Host创建了以下xml:

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

这是控制选项卡主机的类的代码:

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

我的登录活动向Web服务发送用户名和密码,如果Web服务允许用户登录,则应在底部显示带有3个不同活动的Tab Host。如何使用Tab Host显示活动?我试图这样做:

Intent i = new Intent(MainActivity.this, TabBarActivity.class);
startActivity(i);

但它崩溃了。你能救我吗?

更新

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)

2 个答案:

答案 0 :(得分:1)

替换

TabHost tabHost = getTabHost();

TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
tabHost.setup();

答案 1 :(得分:0)

您是否尝试将android:id="@android:id/tabhost"添加到TabHost

<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">

BTW:TabHost似乎已被弃用,您应该使用Fragments。