5

I am trying to implement Tab View for my activity. I am getting an error that states.

java.lang.IllegalArgumentException: you must specify a way to create the tab indicator

     Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab

        // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, MonthActivity.class);

        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("Month")
                      .setContent(intent);
        tabHost.addTab(spec);

        // Do the same for the other tabs
        intent = new Intent().setClass(this, WeekActivity.class);
        spec = tabHost.newTabSpec("Week")
                .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, DayActivity.class);
        spec = tabHost.newTabSpec("Day")
                .setContent(intent);
        tabHost.addTab(spec);
        tabHost.setCurrentTab(2);

This is my xml and I've declared the Activities in the manifest.

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

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="5dp" >

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

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dp" >
        </FrameLayout>
</LinearLayout>

Where am I making my mistake? Thanks for your time and input.

4

2 回答 2

8

尝试这个:

Resources res = getResources(); // Resource object to get Drawables   
tabHost = getTabHost();  // The activity TabHost  
TabHost.TabSpec spec;  // Resusable TabSpec for each tab    
Intent intent;  // Reusable Intent for each tab   

intent = new Intent().setClass(this, First.class);      
spec = tabHost.newTabSpec("First Tab").setIndicator("First Tab",res.getDrawable(R.drawable.icon)).setContent(intent); 
tabHost.addTab(spec);   

intent = new Intent().setClass(this, Second.class); 
spec = tabHost.newTabSpec("Second Tab").setIndicator("Second Tab",res.getDrawable(R.drawable.icon)).setContent(intent); 
tabHost.addTab(spec); 

tabHost.setCurrentTab(1);

并确保您的活动必须是TabActivity您定义 TabHost 的位置。

于 2011-11-14T13:26:18.023 回答
4

一个更明确和准确的答案是您没有在 TabHost.TabSpec 对象上调用 setIndicator 方法,因此如果您将此调用添加到每个选项卡规范对象,您的问题将得到解决。

spec.setIndicator("标签 1");

于 2012-04-28T18:14:29.637 回答