2013-03-11 41 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?

在我链接的文档中,您有一个示例,如果我没有弄错支持库示例中有一些示例。