在Fragment中使用FragmentTabHost时出错

时间:2013-03-11 12:14:02

标签: android android-fragments android-tabhost

我正在尝试在FragmentTabHost内添加Fragment(这是另一个标签小部件的内容。

我使用了以下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>

Fragment的{​​{1}}方法中:

onCreateView()

首先,Igot出现以下错误:

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;

之后我将设置更改为:

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)'?

1 个答案:

答案 0 :(得分:1)

您尚未阅读FragmentTabHost类的文档,该文档明确指出FragmentTabHost特殊TabHost,允许将Fragment对象用于其标签内容。 。因此,您无法将选项卡设置为活动,并且无论如何都没有意义,因为您尝试将活动放在片段中(应该是相反的方式)。

因此修改代码以使用片段作为标签内容或使用TabHost中的普通Activity继续将这些活动用作标签(此选项已弃用,您应该首先使用第一个选项)。

  

是否有正确的方法将标签主机放入片段?

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