我如何使用tabhost?

时间:2012-01-24 21:53:07

标签: android tabs android-tabhost

我想使用标签但它不起作用。 这里是我的.java代码:

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

        TabSpec tab1 = tabHost.newTabSpec("tab1");
        TabSpec tab2 = tabHost.newTabSpec("tab2");
        TabSpec tab3 = tabHost.newTabSpec("tab3");
        TabSpec tab4 = tabHost.newTabSpec("tab4");
        TabSpec tab5 = tabHost.newTabSpec("tab5");
        tab1.setIndicator("menu").setContent(new Intent(this, tab1.class));
        tab2.setIndicator("language").setContent(new Intent(this,tab2.class ));
        tab3.setIndicator("speaking").setContent(new Intent(this,tab3.class));
        tab4.setIndicator("share").setContent(new Intent(this, tab4.class));
        tab5.setIndicator("winnig").setContent(new Intent(this,tab5.class ));
        tabHost.addTab(tab1);
        tabHost.addTab(tab2);
        tabHost.addTab(tab3);
        tabHost.addTab(tab4); 
        tabHost.addTab(tab5);

这里是我的.xml:

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

    <LinearLayout android:id="@+id/LinearLayout01"
        android:orientation="vertical" 
        android:layout_height="fill_parent"
        android:layout_width="fill_parent">

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

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

这是错误:

> 01-24 23:48:47.048: E/AndroidRuntime(19837):
> java.lang.RuntimeException: Unable to start activity
> ComponentInfo{com.tasarimprojesi.donem1/com.tasarimprojesi.donem1.user_manual}:
> java.lang.NullPointerException

1 个答案:

答案 0 :(得分:2)

你应该扩展TabActivity,是吗?

然后

TabHost host = getTabHost();