FragmentManager with FragmentTabHost

时间:2017-03-09 16:01:34

标签: android

我有一个具有四个片段的ViewPager的Activity。其中一个片段必须有两个标签,所以我尝试了FragmentTabHost。

private FragmentTabHost fragmentTabHost;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_zbritjet, container, false);
    fragmentTabHost = (FragmentTabHost) view.findViewById(android.R.id.tabhost);
    fragmentTabHost.setup(view.getContext(), getFragmentManager(), android.R.id.tabcontent);

    fragmentTabHost.addTab(fragmentTabHost.newTabSpec("tab_ofertat").setIndicator("Ofertat"), OfertatItems.class, null);
    fragmentTabHost.addTab(fragmentTabHost.newTabSpec("tab_bizneset").setIndicator("Bizneset"), BiznesetItems.class, null);
    return view;
}

问题是android正在抛出

  

java.lang.IllegalStateException:FragmentManager已在执行事务

堆栈追踪:

  

android.support.v4.app.FragmentManagerImpl.ensureExecReady(FragmentManager.java:1946)                                                                       android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1992)                                                                       android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:762)                                                                       android.support.v4.app.FragmentTabHost.onAttachedToWindow(FragmentTabHost.java:289)

1 个答案:

答案 0 :(得分:1)

尝试使用getChildFragmentManager()代替getFragmentManager()

来自文档:

  

返回一个私有的FragmentManager,用于放置和管理此片段内的片段。