java.lang.IllegalStateException:在活动创建中的FragmentTabHost中的onSaveInstanceState之后无法执行此操作

use*_*098 9 android-fragments

我试图在FragmentActivity中的v4支持库的Fragment中使用FragmentTabHost.调试时会在Activity OnCreate中抛出此异常,因为Alex已指出可能的情况,但仍在活动oncreate中提交片段事务.由于此异常,我的活动显示为带有选项卡的空tabcontent.

注意: - 1)我锁定了屏幕旋转 - 没有方向条件.2)这发生在初始状态中的活动oncreate 3)我在Activity OnCreate()中使用了commit()4)作为日志点,提交关系在TabHost中完成而不是从我这边完成.

这是我的日志: -

03-29 11:32:55.975: E/AndroidRuntime(27470): java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
03-29 11:32:55.975: E/AndroidRuntime(27470):    at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1343)
03-29 11:32:55.975: E/AndroidRuntime(27470):    at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1361)
03-29 11:32:55.975: E/AndroidRuntime(27470):    at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
03-29 11:32:55.975: E/AndroidRuntime(27470):    at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
03-29 11:32:55.975: E/AndroidRuntime(27470):    at android.support.v4.app.FragmentTabHost.onAttachedToWindow(FragmentTabHost.java:282)
03-29 11:32:55.975: E/AndroidRuntime(27470):    at android.view.View.dispatchAttachedToWindow(View.java:9924)
03-29 11:32:55.975: E/AndroidRuntime(27470):    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2198)
03-29 11:32:55.975: E/AndroidRuntime(27470):    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2206)
03-29 11:32:55.975: E/AndroidRuntime(27470):    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2206)
03-29 11:32:55.975: E/AndroidRuntime(27470):    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2206)
03-29 11:32:55.975: E/AndroidRuntime(27470):    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2206)
03-29 11:32:55.975: E/AndroidRuntime(27470):    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2206)
03-29 11:32:55.975: E/AndroidRuntime(27470):    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2206)
03-29 11:32:55.975: E/AndroidRuntime(27470):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1079)
03-29 11:32:55.975: E/AndroidRuntime(27470):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2585)
03-29 11:32:55.975: E/AndroidRuntime(27470):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-29 11:32:55.975: E/AndroidRuntime(27470):    at android.os.Looper.loop(Looper.java:137)
03-29 11:32:55.975: E/AndroidRuntime(27470):    at android.app.ActivityThread.main(ActivityThread.java:4507)
03-29 11:32:55.975: E/AndroidRuntime(27470):    at java.lang.reflect.Method.invokeNative(Native Method)
03-29 11:32:55.975: E/AndroidRuntime(27470):    at java.lang.reflect.Method.invoke(Method.java:511)
03-29 11:32:55.975: E/AndroidRuntime(27470):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
03-29 11:32:55.975: E/AndroidRuntime(27470):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
03-29 11:32:55.975: E/AndroidRuntime(27470):    at dalvik.system.NativeStart.main(Native Method)
Run Code Online (Sandbox Code Playgroud)

这是我的代码

活动:

private void addFragmentToStack(int originCurrentSplitNo, boolean isOrgin,
            int holderId) {

        // Instantiate a new fragment(FragmentTabhost as the layout of this).
        SplitFragment newFragment = SplitFragment.newInstance();


        // Add the fragment to the activity, pushing this transaction
        // on to the back stack.
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(holderId, (Fragment) newFragment,
                newFragment.getCurrentFragmentTag());
        if (null == fragmentTagHolder) {
            fragmentTagHolder = new ArrayList<String>();
        }
    //tag holders in the activity
        fragmentTagHolder.add(newFragment.getCurrentFragmentTag());
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        ft.addToBackStack(null);
        ft.commit();
    }
Run Code Online (Sandbox Code Playgroud)

具有将附加到活动的FragmentTabHost的片段

//SplitFragment.java

private View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        if (container == null) {
            // Currently in a layout without a container, so no
            // reason to create our view.
            return null;
        }


        View fragmentTabHostView = inflater.inflate(R.layout.fragment_tab_host_layout, container,
                false);
        initFrgmentTab(fragmentTabHostView );
        return fragmentTabHostView;
    }

    private View initFrgmentTab(View oemView) {
        if (null == fragmentTabHostView ) {
            return null;
        }

        typeViewHost = (FragmentTabHost) fragmentTabHostView 
                .findViewById(android.R.id.tabhost);

        if (!(null == typeViewHost)) {

            typeViewHost.setup(getActivity().getApplicationContext(), getChildFragmentManager(),android.R.id.tabcontent);

            LinkedHashMap<Integer, fragmentTabContent> frgmentTabContent = ((OEMCarInspectionActivity) getActivity())
                    .getfrgmentTabContent Details();
    Set<Integer> views = frgmentTabContent .getKeySet();
    //Creating the tabs
            for (Integer viewCode : views) {
                fragmentTabContent carView = carTypeViews.get(viewCode );
                String tabTitle = fragmentTabContent.getViewDescription();

                if (null == tabTitle || tabTitle .trim().isEmpty()) {
                    continue;
                }
                Bundle tabViewBundle = new Bundle();
                carViewBundle.putInt(CarViewFragment.CARVIEW_CODE_KEY,
                        viewCode);
                typeViewHost.addTab(typeViewHost.newTabSpec(tabTitle )
                        .setIndicator(tabTitle ), TabViewFragment.class,
                        tabViewBundle);

            }
}
Run Code Online (Sandbox Code Playgroud)

Fragment充当FragmentTabHost的tabcontent:

//TabViewFragment.java
/**
     * The Fragment's UI is just a simple text view showing its instance number.
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        if (null == container) {
            // Currently in a layout without a container, so no
            // reason to create our view.
            return null;
        }

        View v = inflater.inflate(R.layout.tab_view_layout, container,
                false);

        return v;
    }
Run Code Online (Sandbox Code Playgroud)

FragmentTabHost的布局:

//fragment_tab_host_layout.xml(layout for SplitFragment.java )

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="0"
            android:orientation="vertical" >


            <HorizontalScrollView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:fillViewport="true"
                android:scrollbars="none" >

                <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal" />
            </HorizontalScrollView>
        </LinearLayout>

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1" >

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="0dp"
                android:layout_height="0dp" />
        </RelativeLayout>
    </LinearLayout>

</android.support.v4.app.FragmentTabHost>
Run Code Online (Sandbox Code Playgroud)

C B*_*B J 0

最初我认为这很可能是轮换问题。但通过你的补充细节,我想我看到了问题所在。

您正在 onCreateView 的片段中初始化 tabhost。在 onCreateView 中,片段尚未添加到活动中;它只是被要求提供它的布局。会话状态可能尚未恢复(就片段事务管理器而言),因为它本质上处于构建状态的中间(正在插入视图)。

尝试将 tabhost 初始化代码移至 onActivityCreated,该代码在插入视图后调用。IE:

private View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        if (container == null) {
            // Currently in a layout without a container, so no
            // reason to create our view.
            return null;
        }


        View fragmentTabHostView = inflater.inflate(R.layout.fragment_tab_host_layout, container,
                false);

        return fragmentTabHostView;
    }

public void onActivityCreated (Bundle savedInstanceState) {
       initFrgmentTab(getView());
}
Run Code Online (Sandbox Code Playgroud)

如果仍然让人不舒服,请尝试将其移至 onResume (这肯定是在状态恢复之后)。