使用FragmentTabHost错误时,一个片段内 [英] Errors when using a FragmentTabHost inside a Fragment

查看:197
本文介绍了使用FragmentTabHost错误时,一个片段内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林'尝试添加 FragmentTabHost 片段(这是另一个选项卡控件的内容。

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

我用下面的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>

在我的片段 onCreateView()方法:

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以下错误:

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

有没有把标签主机里面有道片段

推荐答案

您还没有读过了的 FragmentTabHost 类,它明确指出, FragmentTabHost 是一个特殊TabHost,允许其标签内容利用碎片对象。的。所以你不能设置的选项卡是活动的,这是没有意义的反正你想在片段(它应该是其他方式)的活动。

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).

所以,修改code使用的片段作为标签内容或在活动使用普通的 TabHost 继续使用这些活动为选项卡(此选项去precated,你应该用真正的第一个选项去)。

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.

这篇关于使用FragmentTabHost错误时,一个片段内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆