java.lang.IllegalStateException:无法在onSaveInstanceState之后执行此操作

时间:2014-07-08 09:35:40

标签: android

我正在开发一个android应用程序。我在片段中使用抽屉。抽屉里有三个菜单。所有工作正常,直到我根据所选菜单在布局中加载片段之前引入密码检查。时间它工作正常,片段为null。当片段不为null时出现问题。当尝试用所选菜单替换现有片段时。当我给出正确的密码时,我得到forceclose。 我正在使用外部课程检查密码,并使用活动结果。 我试着用你的suggetions.help提前帮我。

这是我的班级,

 public class TruckInventoryFragment extends SherlockFragment {
        private DrawerLayout mDrawerLayout;
        private ListView mDrawerList;
        private ActionBarDrawerToggle mDrawerToggle;
        Fragment fragment;
        private CharSequence mDrawerTitle;
        private CharSequence mTitle;
        private String[] navMenuTitles;
        private TypedArray navMenuIcons;
        private ArrayList<NavDrawerItem> navDrawerItems;
        private NavDrawerListAdapter adapter;
        int pos;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Get the view from fragmenttab2.xml
        View view = inflater.inflate(R.layout.truckinventoryfragment,
                container, false);
        return view;
    }



    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);

        mTitle = mDrawerTitle = getActivity().getTitle();

        // load slide menu items
        navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);

        // nav drawer icons from resources
        navMenuIcons = getResources()
                .obtainTypedArray(R.array.nav_drawer_icons);

        mDrawerLayout = (DrawerLayout) getActivity().findViewById(
                R.id.drawer_layout);
        mDrawerList = (ListView) getActivity().findViewById(
                R.id.list_slidermenu);
        navDrawerItems = new ArrayList<NavDrawerItem>();

            navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons
                .getResourceId(0, -1)));
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons
                .getResourceId(1, -1)));
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons
                .getResourceId(2, -1)));

        // Recycle the typed array
        navMenuIcons.recycle();

        mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

        // setting the nav drawer list adapter
        adapter = new NavDrawerListAdapter(getActivity()
                .getApplicationContext(), navDrawerItems);
        mDrawerList.setAdapter(adapter);


        mDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout,
                R.drawable.ic_drawer, // nav menu toggle icon
                R.string.app_name, // nav drawer open - description for
                                    // accessibility
                R.string.app_name // nav drawer close - description for
                                    // accessibility
        ) {
            public void onDrawerClosed(View view) {
                // getActionBar().setTitle(mTitle);
                // calling onPrepareOptionsMenu() to show action bar icons
                // invalidateOptionsMenu();
            }

            public void onDrawerOpened(View drawerView) {
                // getActionBar().setTitle(mDrawerTitle);
                // // calling onPrepareOptionsMenu() to hide action bar icons
                // invalidateOptionsMenu();
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);


    }

    /**
     * Slide menu item click listener
     * */
    private class SlideMenuClickListener implements
            ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            // display view for selected nav drawer item
            displayView(position);
        }
    }


    public void checkfragment()
    {
        if (fragment != null) {
            FragmentManager fragmentManager = getChildFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.truckinventory_form_fragmentgroup, fragment)
                    .commit();

            // update selected item and title, then close the drawer
            mDrawerList.setItemChecked(pos, true);
            mDrawerList.setSelection(pos);
            setTitle(navMenuTitles[pos]);
            mDrawerLayout.closeDrawer(mDrawerList);
        } else {
            // error in creating fragment
            Log.e("MainActivity", "Error in creating fragment");
        }
    }
    private void displayView(int position) {
        // update the main content by replacing fragments
        fragment = null;
        pos = position;
        Intent i;
        switch (position) {
        case 0:
    //      fragment = new FragmentLoadOut();
            i = new Intent(getActivity(), RoutePasswords.class);
            i.putExtra("activity", "LoadOutPass");
            startActivityForResult(i, 11);
            break;

        case 1:
            i = new Intent(getActivity(), RoutePasswords.class);
            i.putExtra("activity", "LoadTransferPass");
            startActivityForResult(i, 12);
    //      fragment = new FragmentLoadtransfer();

            break;

        case 2:
            i = new Intent(getActivity(), RoutePasswords.class);
            i.putExtra("activity", "ViewStockPass");
            startActivityForResult(i, 13);
        //  fragment = new FragmentStockview();

            break;

        default:
            break;
        }
        checkfragment();

    }

    public void setTitle(CharSequence title) {
        mTitle = title;
        // getSupportActionBar().setTitle(mTitle);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.d("nzm", "TruckInventoryFragment");
        Log.d("nzm", "" + data + "requestCode" + requestCode);

        //super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
        case 11:
            if (data.getBooleanExtra("password", true)) {
                Log.d("nzm",
                        "onActivityResult:bolvalue"
                                + data.getBooleanExtra("password", true));

                fragment = new FragmentLoadOut();
                checkfragment();

            }

            break;

        case 12:
            if (data.getBooleanExtra("password", true)) {
                Log.d("nzm",
                        "onActivityResult:bolvalue"
                                + data.getBooleanExtra("password", true));

                fragment = new FragmentLoadtransfer();
                checkfragment();

            }
            break;
        case 13:
            if (data.getBooleanExtra("password", true)) {
                Log.d("nzm",
                        "onActivityResult:bolvalue"
                                + data.getBooleanExtra("password", true));

                fragment = new FragmentStockview();
                checkfragment();
            }
            break;
        default:
            break;

        }

    }

}

我的LOGCAT

   07-08 14:48:06.398: W/dalvikvm(22927): threadid=1: thread exiting with uncaught exception (group=0x40aac228)
07-08 14:48:06.418: E/AndroidRuntime(22927): FATAL EXCEPTION: main
07-08 14:48:06.418: E/AndroidRuntime(22927): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=131084, result=-1, data=Intent { (has extras) }} to activity {com.winwrench.pos/com.winwrench.pos.HomeActivity}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
07-08 14:48:06.418: E/AndroidRuntime(22927):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3398)
07-08 14:48:06.418: E/AndroidRuntime(22927):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3448)
07-08 14:48:06.418: E/AndroidRuntime(22927):    at android.app.ActivityThread.access$1100(ActivityThread.java:139)
07-08 14:48:06.418: E/AndroidRuntime(22927):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
07-08 14:48:06.418: E/AndroidRuntime(22927):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-08 14:48:06.418: E/AndroidRuntime(22927):    at android.os.Looper.loop(Looper.java:156)
07-08 14:48:06.418: E/AndroidRuntime(22927):    at android.app.ActivityThread.main(ActivityThread.java:4987)
07-08 14:48:06.418: E/AndroidRuntime(22927):    at java.lang.reflect.Method.invokeNative(Native Method)
07-08 14:48:06.418: E/AndroidRuntime(22927):    at java.lang.reflect.Method.invoke(Method.java:511)
07-08 14:48:06.418: E/AndroidRuntime(22927):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-08 14:48:06.418: E/AndroidRuntime(22927):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-08 14:48:06.418: E/AndroidRuntime(22927):    at dalvik.system.NativeStart.main(Native Method)
07-08 14:48:06.418: E/AndroidRuntime(22927): Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
07-08 14:48:06.418: E/AndroidRuntime(22927):    at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1343)
07-08 14:48:06.418: E/AndroidRuntime(22927):    at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1361)
07-08 14:48:06.418: E/AndroidRuntime(22927):    at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
07-08 14:48:06.418: E/AndroidRuntime(22927):    at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
07-08 14:48:06.418: E/AndroidRuntime(22927):    at com.winwrench.pos.fragments.TruckInventoryFragment.checkfragment(TruckInventoryFragment.java:132)
07-08 14:48:06.418: E/AndroidRuntime(22927):    at com.winwrench.pos.fragments.TruckInventoryFragment.onActivityResult(TruckInventoryFragment.java:213)
07-08 14:48:06.418: E/AndroidRuntime(22927):    at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:166)
07-08 14:48:06.418: E/AndroidRuntime(22927):    at com.winwrench.pos.HomeActivity.onActivityResult(HomeActivity.java:390)
07-08 14:48:06.418: E/AndroidRuntime(22927):    at android.app.Activity.dispatchActivityResult(Activity.java:4747)
07-08 14:48:06.418: E/AndroidRuntime(22927):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3394)
07-08 14:48:06.418: E/AndroidRuntime(22927):    ... 11 more

2 个答案:

答案 0 :(得分:1)

你应该检查onActivityResult中的data!=null。将以下代码替换为onActvitiyResult,然后参阅。

@Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            Log.d("nzm", "TruckInventoryFragment");
            Log.d("nzm", "" + data + "requestCode" + requestCode);

            //super.onActivityResult(requestCode, resultCode, data);
            if(data==null)
            {
                  Log.d("nzm", "Failed to deliver result");
                  return;
            }


            switch (requestCode) {
            case 11:
                if (data.getBooleanExtra("password", true)) {
                    Log.d("nzm",
                            "onActivityResult:bolvalue"
                                    + data.getBooleanExtra("password", true));

                    fragment = new FragmentLoadOut();
                    checkfragment();

                }

                break;

            case 12:
                if (data.getBooleanExtra("password", true)) {
                    Log.d("nzm",
                            "onActivityResult:bolvalue"
                                    + data.getBooleanExtra("password", true));

                    fragment = new FragmentLoadtransfer();
                    checkfragment();

                }
                break;
            case 13:
                if (data.getBooleanExtra("password", true)) {
                    Log.d("nzm",
                            "onActivityResult:bolvalue"
                                    + data.getBooleanExtra("password", true));

                    fragment = new FragmentStockview();
                    checkfragment();
                }
                break;
            default:
                break;

            }

        }

答案 1 :(得分:1)

更改

fragmentManager.beginTransaction().
replace(R.id.truckinventory_form_fragmentgroup, fragment).commit();

fragmentManager.beginTransaction().
replace(R.id.truckinventory_form_fragmentgroup, fragment).commitAllowingStateLoss();

这是一个已知的错误,如this question所述。