选项卡例外:“您必须指定创建选项卡指示器的方法”

时间:2011-08-18 13:56:51

标签: android exception tabs

我的应用程序包含三个选项卡,其中两个包含ListView,另一个只是TextView的活动。要使用Tabs获取主要活动,我使用developers.anroid.com http://developer.android.com/resources/tutorials/views/hello-tabwidget.html中的示例 根据这个例子,我的布局是:

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

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

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


</LinearLayout>

</TabHost>

所以,下一个代码是活动代码(只有两页):

public class MainActivity extends TabActivity {



@Override
public void onCreate(Bundle savedInstanceState) 
{

    super.onCreate(savedInstanceState);
    setContentView(R.layout.tabs_layout_start_page);

    Resources res = getResources();
    TabHost tabHost = getTabHost();
    TabHost.TabSpec spec;
    Intent intent;

    intent = new Intent(this, ListViewTab1Activity.class);
    spec = tabHost.newTabSpec("Page1").setIndicator("Page 1", res.getDrawable(R.drawable.icon))
    .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent(this, ListViewTab2Activity.class);
    spec = tabHost.newTabSpec("Page 2").setIndicator("Page 2", res.getDrawable(R.drawable.icon))
    .setContent(intent);
    tabHost.addTab(spec);

    tabHost.setCurrentTab(1);

}

}

每个意图都指向另一个活动,它在布局中使用硬编码ListView,并从资源的字符串数组中定义它。但是在开始之后我得到了例外 IllegalArgument异常“”你必须指定一种方法来创建标签指示符。“它让我停下来,拜托,帮助我。 谢谢大家

2 个答案:

答案 0 :(得分:1)

    Intent i;
    i = new Intent().setClass(this, Tab1.class);
    TabSpec sp = th.newTabSpec("Tab1");
    sp.setIndicator("alma");
    sp.setContent(i);
    th.addTab(sp);

    i = new Intent().setClass(this, Tab2.class);
    sp = th.newTabSpec("Tab2");
    sp.setIndicator("alma2");
    th.addTab(sp);
    th.setCurrentTab(0);

此代码完美无缺,因此您应该尝试制作 此

spec = tabHost.newTabSpec("Page 2").setIndicator("Page 2", res.getDrawable(R.drawable.icon))

像我一样分开。也许shoudn`t使用单个命令行来设置attrs。

答案 1 :(得分:1)

我刚遇到同样的问题。

我发现我的问题是重复使用sp

我这样做是为了解决问题:

TabSpec spec1=tabHost.newTabSpec("Tab 1");
    spec1.setContent(R.id.tab1);
    spec1.setIndicator("Tab 1",getResources().getDrawable(R.drawable.icon_tab1_config));

TabSpec spec2=tabHost.newTabSpec("Tab 2");
    spec2.setIndicator("Tab 2",getResources().getDrawable(R.drawable.icon_tab2_config));
    spec2.setContent(R.id.tab2);

使用不同的TabSpec值为我解决了这个问题。