TabHost没有扩展TabActivity

时间:2013-05-01 08:16:00

标签: android android-tabhost

Android 2.3.3

我只想试试如何在不扩展TabActivity的情况下实现TabHost。我收到了IllegalArgumentException。请看下面的代码。我不想做任何事情,只需在TabHost中添加3个标签。

Logcat :::

的异常
05-01 13:41:21.404: E/AndroidRuntime(362): FATAL EXCEPTION: main
05-01 13:41:21.404: E/AndroidRuntime(362): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tabhostdemo/com.example.tabhostdemo.MainActivity}: java.lang.IllegalArgumentException: you must specify a way to create the tab content
05-01 13:41:21.404: E/AndroidRuntime(362):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-01 13:41:21.404: E/AndroidRuntime(362):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-01 13:41:21.404: E/AndroidRuntime(362):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-01 13:41:21.404: E/AndroidRuntime(362):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-01 13:41:21.404: E/AndroidRuntime(362):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-01 13:41:21.404: E/AndroidRuntime(362):  at android.os.Looper.loop(Looper.java:123)
05-01 13:41:21.404: E/AndroidRuntime(362):  at android.app.ActivityThread.main(ActivityThread.java:3683)
05-01 13:41:21.404: E/AndroidRuntime(362):  at java.lang.reflect.Method.invokeNative(Native Method)
05-01 13:41:21.404: E/AndroidRuntime(362):  at java.lang.reflect.Method.invoke(Method.java:507)
05-01 13:41:21.404: E/AndroidRuntime(362):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-01 13:41:21.404: E/AndroidRuntime(362):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-01 13:41:21.404: E/AndroidRuntime(362):  at dalvik.system.NativeStart.main(Native Method)
05-01 13:41:21.404: E/AndroidRuntime(362): Caused by: java.lang.IllegalArgumentException: you must specify a way to create the tab content
05-01 13:41:21.404: E/AndroidRuntime(362):  at android.widget.TabHost.addTab(TabHost.java:202)
05-01 13:41:21.404: E/AndroidRuntime(362):  at com.example.tabhostdemo.MainActivity.onCreate(MainActivity.java:22)
05-01 13:41:21.404: E/AndroidRuntime(362):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-01 13:41:21.404: E/AndroidRuntime(362):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-01 13:41:21.404: E/AndroidRuntime(362):  ... 11 more

MainActivity.Java :::

public class MainActivity extends Activity {

    private TabHost tabhost;
    private TabHost.TabSpec tabspec;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        tabspec = tabhost.newTabSpec("SCAN");
        tabspec.setIndicator("Scan");
        tabhost.addTab(tabspec);

        tabspec = tabhost.newTabSpec("CREATE");
        tabspec.setIndicator("Create");
        tabhost.addTab(tabspec);

        tabspec = tabhost.newTabSpec("MORE");
        tabspec.setIndicator("More");
        tabhost.addTab(tabspec);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

activity_main.xml中:::

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TabHost
        android:id="@+id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

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

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

</RelativeLayout>

任何人都可以告诉我这个错误是什么意思 - you must specify a way to create the tab content以及如何指定它?

2 个答案:

答案 0 :(得分:2)

如果不扩展TabActivity,您还可以在xml文件中实现带有修改的标签栏。

 <TabWidget
        android:id="@android:id/tabs"

 <FrameLayout
        android:id="@android:id/tabcontent"

现在初始化tabhost并以这种方式向tabhost添加标签。

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


        tab_host.setup();
        // Set Tab Specification for Battery Tab

        TabSpec battery_tab_spec = tab_host.newTabSpec("assignments_tab");

        battery_tab_spec.setContent(R.id.assignments_tab);
        battery_tab_spec.setIndicator(makeIndicator(getResources().getDrawable(
                R.drawable.dashboard_assignments_student)));
        tab_host.addTab(battery_tab_spec);

        // Set Tab Specification for Network Tab
        TabSpec network_tab_spec = tab_host.newTabSpec("discussions_tab");

        network_tab_spec.setContent(R.id.discussions_tab);
        network_tab_spec.setIndicator(makeIndicator(getResources().getDrawable(
                R.drawable.dashboard_discuss)));
        tab_host.addTab(network_tab_spec);

        // Set Tab Specification for Device Tab
        TabSpec device_tab_spec = tab_host.newTabSpec("progress_tab");

        device_tab_spec.setContent(R.id.progress_tab);
        device_tab_spec.setIndicator(makeIndicator(getResources().getDrawable(
                R.drawable.dashboard_track)));
        tab_host.addTab(device_tab_spec);

试试这个。

答案 1 :(得分:0)

您应该将MainActivity延长至TabActivity而不是Activity,并确保您已在xml中定义TabWidgetTabHost,如下所示

 <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:background="@drawable/background_login"
android:layout_height="fill_parent">

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