在onSaveInstanceState WorkAround之后无法执行此操作

时间:2013-09-16 09:56:07

标签: java android

这是例外

09-16 11:48:48.214: W/dalvikvm(3644): threadid=1: thread exiting with uncaught exception (group=0x415c3700)
09-16 11:48:48.214: E/AndroidRuntime(3644): FATAL EXCEPTION: main
    09-16 11:48:48.214: E/AndroidRuntime(3644): java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
    09-16 11:48:48.214: E/AndroidRuntime(3644):     at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1343)
    09-16 11:48:48.214: E/AndroidRuntime(3644):     at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1354)
    09-16 11:48:48.214: E/AndroidRuntime(3644):     at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
    09-16 11:48:48.214: E/AndroidRuntime(3644):     at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
    09-16 11:48:48.214: E/AndroidRuntime(3644):     at com.smedilepaolo.newsfeed.SubscriptionsListActivity.apriAllUnreadItems(SubscriptionsListActivity.java:159)
    09-16 11:48:48.214: E/AndroidRuntime(3644):     at com.smedilepaolo.newsfeed.SubscriptionsListFragment$GetSubscriptionListTask.onPostExecute(SubscriptionsListFragment.java:466)
    09-16 11:48:48.214: E/AndroidRuntime(3644):     at com.smedilepaolo.newsfeed.SubscriptionsListFragment$GetSubscriptionListTask.onPostExecute(SubscriptionsListFragment.java:1)
    09-16 11:48:48.214: E/AndroidRuntime(3644):     at android.os.AsyncTask.finish(AsyncTask.java:631)
    09-16 11:48:48.214: E/AndroidRuntime(3644):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
    09-16 11:48:48.214: E/AndroidRuntime(3644):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
    09-16 11:48:48.214: E/AndroidRuntime(3644):     at android.os.Handler.dispatchMessage(Handler.java:99)
    09-16 11:48:48.214: E/AndroidRuntime(3644):     at android.os.Looper.loop(Looper.java:137)
    09-16 11:48:48.214: E/AndroidRuntime(3644):     at android.app.ActivityThread.main(ActivityThread.java:5103)
    09-16 11:48:48.214: E/AndroidRuntime(3644):     at java.lang.reflect.Method.invokeNative(Native Method)
    09-16 11:48:48.214: E/AndroidRuntime(3644):     at java.lang.reflect.Method.invoke(Method.java:525)
    09-16 11:48:48.214: E/AndroidRuntime(3644):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
    09-16 11:48:48.214: E/AndroidRuntime(3644):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    09-16 11:48:48.214: E/AndroidRuntime(3644):     at dalvik.system.NativeStart.main(Native Method)
    09-16 11:48:59.315: I/Process(3644): Sending signal. PID: 3644 SIG: 9

这是导致它的代码

public void apriAllUnreadItems(){

    Log.d(TAG, "apriAllUnreadItems");

    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();

    Fragment oldDetail = fm.findFragmentById(R.id.detailFragmentContainer);
    Fragment newDetail;

    isTablet = getResources().getBoolean(R.bool.isTablet);
    if(isTablet) newDetail=(Fragment) NewsListFragment_Tablet.newInstance(0, 0);
    else newDetail=(Fragment) NewsListFragment_Phone.newInstance(0, 0);

    if (oldDetail != null) {
        ft.remove(oldDetail);
    }

    ft.add(R.id.detailFragmentContainer, newDetail);
    ft.commit();
}

* 编辑AsyncTask

/************************************************
         *          GetSubscriptionListTask
         ************************************************/

        private class GetSubscriptionListTask extends AsyncTask<Void, Void, Void> {

            boolean onlyUnread=true;

            public GetSubscriptionListTask(boolean onlyUnread) {
                super();
                this.onlyUnread=onlyUnread;
            }

            ProgressDialog progress;

            @Override
              protected void onPreExecute() {
                //Show progress Dialog here
                  super.onPreExecute();

                  // create ProgressDialog here ...
                  progress = new ProgressDialog(getActivity());
                  progress.setMessage("Downloading Subscriptions");
                  // set other progressbar attributes

                  progress.setCancelable (false);
                  progress.setIndeterminate (true);
                  progress.show();

                }


            @Override
            protected Void doInBackground(Void... params) {

                if(this.isCancelled()==false){
                reader.getSubscriptions(this.onlyUnread);
                }

                return null;

            }

            @Override
            protected void onPostExecute(Void v) {

                /************************************************
                 *       Subscriptions Adapter - ListView
                 ************************************************/



                // preparing list data
                prepareListData();

                listAdapter = new ExpandableListAdapter(getActivity(), listDataHeader, listDataChild);

                // setting list adapter
                expListView.setAdapter(listAdapter);

             // setting list adapter
                expListView.setAdapter(listAdapter);


                expListView.expandGroup(0);
                expListView.expandGroup(1);
                expListView.expandGroup(2);





                mCallbacks.apriAllUnreadItems();

                  //Dismiss Progress Dialog here
                progress.dismiss();

                // Listview on child click listener
                expListView.setOnChildClickListener(new OnChildClickListener() {

                    @Override
                    public boolean onChildClick(ExpandableListView parent, View v,
                            int groupPosition, int childPosition, long id) {

                        Toast.makeText(
                                getActivity(),
                                listDataHeader.get(groupPosition)
                                        + " : "
                                        + listDataChild.get(
                                                listDataHeader.get(groupPosition)).get(
                                                childPosition), Toast.LENGTH_SHORT)
                                .show();



                        final SubscriptionsArray element= listDataChild.
                                get(listDataHeader.get(groupPosition)).
                                get(childPosition);



                        if(setSourceAsReadToggle.isChecked()==true){
                            //Chiedo all'utente se vuole settare la sorgente che ha selezionato come letta
                            displayAlertDialog(element);


                        }
                        else{
                            //Carico News elemento selezionato
                        mCallbacks.onSubscriptionSelected(groupPosition, childPosition);
                        }



                        return false;
                    }
                });


            }
          }

1 个答案:

答案 0 :(得分:1)

使用commitAllowingStateLoss()解决;而不是commit();

由于