我在单个活动中使用两个标签主机。一个用于导航抽屉,另一个用于我的活动布局。
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TabHost
android:id="@+id/customTabHost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:textColor="@android:color/black" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ViewFlipper
android:id="@+id/view_flipper_one"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ViewFlipper>
<ViewFlipper
android:id="@+id/view_flipper_two"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ViewFlipper>
<ViewFlipper
android:id="@+id/view_flipper_three"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ViewFlipper>
</FrameLayout>
</LinearLayout>
</TabHost>
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="start"
android:layout_weight=".8"
android:background="@android:color/white"
android:textColor="@android:color/black" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
<Button
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is another button" >
</Button>
<TextView
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is a text" >
</TextView>
</FrameLayout>
</LinearLayout>
</TabHost>
在我的活动中 -
TabHost host = (TabHost) findViewById(android.R.id.tabhost);
TabSpec spec = host.newTabSpec("tab1");
spec.setContent(R.id.list);
spec.setIndicator("Button1");
host.addTab(spec);
spec = host.newTabSpec("tab2");
spec.setContent(R.id.tab2);
spec.setIndicator("Next Button");
host.addTab(spec);
spec = host.newTabSpec("tab3");
spec.setContent(R.id.tab3);
spec.setIndicator("Just some text");
host.addTab(spec);
TabHost flipperHost = (TabHost) findViewById(R.id.customTabHost);
TabSpec flipperSpec = flipperHost.newTabSpec("tab4");
flipperSpec.setContent(R.id.view_flipper_one);
flipperSpec.setIndicator(getString(R.string.featured_reports));
flipperHost.addTab(flipperSpec);
flipperSpec = flipperHost.newTabSpec("tab5");
flipperSpec.setContent(R.id.view_flipper_two);
flipperSpec.setIndicator(getString(R.string.global_trends));
flipperHost.addTab(flipperSpec);
flipperSpec = flipperHost.newTabSpec("tab6");
flipperSpec.setContent(R.id.view_flipper_three);
flipperSpec.setIndicator(getString(R.string.tcs_perspectives));
flipperHost.addTab(flipperSpec);
但它在第二个标签主机规范行中给出了空指针异常
flipperSpec.setContent(R.id.view_flipper_one);
我已将第二个标签主机的ID更改为R.id.customTabHost,因为我想在同一个活动中同时使用两个标签主机。我不知道问题出在哪里?
提前致谢