4

我试图拥有一个包含在片段中的标签主机,其中标签内容由片段定义。好吧,

我从谷歌的FragmentsTab示例开始。这显示了如何将片段用作选项卡内容,但其TabHost本身仍托管在活动中。同样,我需要将标签主机托管在片段中。

我举了个FragmentsTab例子,把代码onCreate()移到onCreateView()我的Fragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View fragmentView = inflater.inflate(R.layout.detail_fragment, container);

    mTabHost = (TabHost) fragmentView.findViewById(android.R.id.tabhost);
    mTabHost.setup();

    mTabManager = new TabManager(getActivity(), mTabHost, R.id.realtabcontent);

    // TODO: localize
    mTabManager.addTab(mTabHost.newTabSpec("info").setIndicator("File Info"), InfoFragment.class, null);

    return fragmentView;
}

当我跑步时,我得到了这个异常,

05-25 14:42:27.272: E/AndroidRuntime(4701): Caused by: java.lang.IllegalStateException: Recursive entry to executePendingTransactions
05-25 14:42:27.272: E/AndroidRuntime(4701):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1343)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:429)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at com.zimbra.octopus.android.TabManager.onTabChanged(TabManager.java:116)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at android.widget.TabHost.invokeOnTabChangeListener(TabHost.java:359)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at android.widget.TabHost.setCurrentTab(TabHost.java:344)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at android.widget.TabHost.addTab(TabHost.java:216)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at com.zimbra.octopus.android.TabManager.addTab(TabManager.java:92)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at com.zimbra.octopus.android.DetailFragment.onCreateView(DetailFragment.java:46)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:819)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1053)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1371)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:429)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at com.zimbra.octopus.android.TabManager.onTabChanged(TabManager.java:116)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at android.widget.TabHost.invokeOnTabChangeListener(TabHost.java:359)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at android.widget.TabHost.setCurrentTab(TabHost.java:344)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at android.widget.TabHost.addTab(TabHost.java:216)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at com.zimbra.octopus.android.TabManager.addTab(TabManager.java:92)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at com.zimbra.octopus.android.DetailFragment.onCreateView(DetailFragment.java:46)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:819)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1111)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:264)
05-25 14:42:27.272: E/AndroidRuntime(4701):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)

查看跟踪,我看到onCreateView()DetailFragment(承载TabHost)调用的片段递归。不知何故,对TabManager.addTab()(再次参见 FragmentsTab 示例)的调用导致对DetailFragment.onCreateView().

我究竟做错了什么?这甚至可能吗?

4

2 回答 2

2

Android 不允许您将一个片段嵌入到其他片段中。您的 logcat 表明您正在另一个片段事务中执行一个片段事务(即递归条目到 executePendingTransactions)。只是不允许,对不起!

于 2012-05-25T22:02:03.003 回答
0

在设置 TabHost 之前添加:

LocalActivityManager mLocalActivityManager = new LocalActivityManager(getActivity(), false);
mLocalActivityManager.dispatchCreate(savedInstanceState);
mTabHost.setup(mLocalActivityManager);
于 2012-06-12T15:14:55.240 回答