正如标题所述,我正在尝试使用包含在片段中的tabhost,其中标签内容由片段定义.好吧,
我从谷歌的FragmentsTab例子开始.这显示了如何使用片段作为选项卡内容,但TabHost本身仍然托管在一个活动中.再次,我需要将标签主机托管在片段中.
我拿了FragmentsTab示例,并将代码移入onCreate()并将其移动到我的Fragment中的onCreateView(),
@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()的递归调用.
我究竟做错了什么?这有可能吗?
谢谢.
最佳答案 Android不允许您在其他片段中嵌入一个片段.您的logcat表示您正在另一个中执行一个片段事务(即,对executePendingTransactions进行递归输入).这是不允许的,对不起!