2011-08-29 52 views
0

如何在tabhost創建多個tabwidget,我得到空指針異常,當我這樣做多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> 

回答

1

你並不需要一個第二TabWidget在那裏。

如果你問增加超過1片到TabHost請參見本教程:http://developer.android.com/resources/tutorials/views/hello-tabwidget.html

檢查第6步認真創建newTabSpec()的位置。

+0

我試圖添加多則tabspec,但我得到了一個錯誤,可以請你給我一個簡單的〔實施例 – insatien

+0

的我發佈的鏈接是一個簡單的例子。 – C0deAttack

+0

我已經看到了這個例子,它是有幫助的,但它只創建一個tabhost,我想有兩個tabhost一個在頂部和一個在buttom中操縱相同的FrameLayout – insatien

1

要添加多個選項卡,您必須創建tabSpec對象,而不是使用TabbHost的.addTab()方法添加每個tabspec。

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 

我跳它會幫...