Errors when using a FragmentTabHost inside a Fragment

我的梦境 提交于 2019-12-12 02:24:43

问题


Im' trying to add a FragmentTabHost inside a Fragment(which is the content of another tab widget.

I used the following 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>

And in my Fragment's onCreateView() method:

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;

At first Igot the following error:

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

Afterwards I've changed the setup to:

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

And I got this error instead:

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

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


回答1:


You haven't read the documentation for the FragmentTabHost class which clearly states that FragmentTabHost is a Special TabHost that allows the use of Fragment objects for its tab content.. So you can't setup the tabs to be activities and it would not make sense anyway as you're trying to have activities in fragments(it should be the other way around).

So modify your code to use fragments as the tabs content or use a normal TabHost in an Activity to continue using those activities as tabs(this option is deprecated and you should really go with the first option).

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

In the documentation I've linked you have an example, if I'm not mistaken there are some examples in the support library's samples.



来源:https://stackoverflow.com/questions/15338320/errors-when-using-a-fragmenttabhost-inside-a-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!