如何在一个tabhost中创建多个tabwidget,当我这样做时,我会得到空指针异常
<?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: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="0dip"
android:layout_weight="1" />
<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
</LinearLayout>
</TabHost> 发布于 2011-08-29 20:03:41
你不需要第二个TabWidget在那里。
如果您询问有关向TabHost添加多个选项卡的问题,请参阅本教程:http://developer.android.com/resources/tutorials/views/hello-tabwidget.html
仔细检查步骤6中创建newTabSpec()的位置。
发布于 2015-11-06 19:05:46
要添加多个选项卡,您必须创建tabSpec对象,然后使用TabbHost的.addTab()方法添加每个选项卡规范。
示例
myTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
myTabHost.setup(this,getsupportFragmentManager(),android.R.id.tabcontent);
myTabHost.addTab(myTabHost.newTabSpec("tab1").setIndicator("Tab1",null), SomeFragmentClass.class,null);
myTabHost.addTab(myTabHost.newTabSpec("tab2").setIndicator("Tab2",null), AnotherFragmentClass.class,null); // just pass fragment classes with it我希望这会有帮助..。
https://stackoverflow.com/questions/7229738
复制相似问题