2013-03-11 47 views
0

林」嘗試添加FragmentTabHost一個Fragment內(這是另一個選項卡窗口小部件的內容內的FragmentTabHost時錯誤使用片段

我用下面的XML:

<android.support.v4.app.FragmentTabHost 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@android:id/tabhost" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
    <LinearLayout 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 
     <FrameLayout 
       android:id="@android:id/tabcontent" 
       android:layout_width="0dp" 
       android:layout_height="0dp" 
       android:layout_weight="0"/> 
     <FrameLayout 
       android:id="@+id/realtabcontent" 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_weight="1"/> 
     <TabWidget 
       android:id="@android:id/tabs" 
       android:orientation="horizontal" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="0"/> 

    </LinearLayout> 
</android.support.v4.app.FragmentTabHost> 

而在我FragmentonCreateView()方法:

View basicSearchView = inflater.inflate(R.layout.search_layout, container, false); 
     try { 
     mTabHost = (FragmentTabHost) basicSearchView.findViewById(android.R.id.tabhost); 
     LocalActivityManager mLocalActivityManager = new LocalActivityManager(this, false); 
     mTabHost.setup(mLocalActivityManager); 

     TabHost.TabSpec tab = mTabHost.newTabSpec("my tab content"); 

     tab.setContent(new Intent(getActivity(), JoinActivity.class)); 
     tab.setIndicator("Test", getResources().getDrawable(R.drawable.search_pheeds_selector)); 
     mTabHost.addTab(tab); 
     } 
     catch (Exception e) { 
      Log.e("Udi",e.getMessage()); 
     } 

     return basicSearchView; 

起初IGOT以下錯誤:

ERROR/Udi(25726): Must call setup() that takes a Context and FragmentManager 

之後我已經改變了設置到:

mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent); 

而且我得到了這個錯誤,而不是:

ERROR/Udi(25996): Did you forget to call 'public void setup(LocalActivityManager activityGroup)'? 

有沒有把Fragment內接頭主機有道?

回答

1

您尚未閱讀FragmentTabHost類的文檔,該文檔明確指出FragmentTabHost特殊TabHost,允許使用Fragment對象作爲其選項卡內容。。所以你不能將選項卡設置爲活動,無論如何,因爲你試圖在片段中進行活動(它應該是相反的方式)。

所以修改代碼以使用片段作爲標籤內容或使用正常的TabHostActivity繼續使用這些活動選項卡(此選項已被廢棄,你應該與第一個選項去)。

Is there a proper way to put tab host inside a Fragment?

在我鏈接的文檔中,您有一個示例,如果我沒有弄錯支持庫示例中有一些示例。