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

发布于 2024-11-30 15:05:29 字数 1944 浏览 1 评论 0原文

我的应用程序由三个选项卡组成,其中两个具有 ListView,一个只是具有 TextView 的活动。为了通过选项卡获取主要活动,我使用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 Exception“”您必须指定一种创建选项卡指示器的方法”。这让我停下来,请帮助我。 谢谢大家

My application consists three tabs and two of them have the ListView and one is just activity with TextView. To get main activity with Tabs I use the example from developers.anroid.com http://developer.android.com/resources/tutorials/views/hello-tabwidget.html
According to this example my layout is:

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

So, next code is code of activity (there are only two pages):

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

}

}

Every intent is pointed to another activity, which has hard-coded ListView in layout and code to define it from string-array from resources. But after starting I get exception
IllegalArgument Exception ""you must specify a way to create tab indicator". and it makes me stopped, please, help me.
thanks for everyone

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若水般的淡然安静女子 2024-12-07 15:05:29
    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))

就像我所做的那样, 被分开了。也许不应该使用单个命令行来设置属性。

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

This code is works perfectly, so you should try to make
this

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

get separated as I had done. Maybe shoudn`t use single command line to set attrs.

青巷忧颜 2024-12-07 15:05:29

我刚刚遇到了同样的问题。

我发现我的问题是 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 值解决了我的问题。

I just had this same problem.

I found my problem was the reuse of sp

I did this to solve the problem:

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

using different TabSpec values fixed the issue for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文