9

Fragments API发布后,我开始使用兼容性软件包将所有弃用的对话框移植到DialogFraments中。一切都运行良好,直到我发现我的对话都会导致崩溃只有ICS:显示DialogFragments崩溃ICS

E/AndroidRuntime( 883): FATAL EXCEPTION: main 
E/AndroidRuntime( 883): java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState 
E/AndroidRuntime( 883): at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1254) 
E/AndroidRuntime( 883): at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1265) 
E/AndroidRuntime( 883): at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:541) 
E/AndroidRuntime( 883): at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:525) 
E/AndroidRuntime( 883): at android.support.v4.app.DialogFragment.show(DialogFragment.java:123) 
E/AndroidRuntime( 883): at com.myapp.ui.dialogs.TwoButtonDialogFragment.showDialog(TwoButtonDialogFragment.java:84) 

我的对话,以便显示HTTP响应给用户显示在AsyncTask.onPostExecute()。在深入研究这个问题之后,我得出这样的结论:只有在活动暂停或停止时才会发生此异常,并且在其他版本的Android上不会发生。 我试过使用commitAllowingStateLoss(),但它没有帮助,因为在DialogFragment.show()上抛出异常。这里是我的DialogFragment代码:

private static void showDialog(FragmentActivity activity, String title, String msg, 
     String positiveButtonText, String negativeButtonText, int id, Bundle args) { 

    if (activity.isFinishing()) { 
     return; 
    } 

    FragmentManager fmgr = activity.getSupportFragmentManager(); 
    FragmentTransaction ft = fmgr.beginTransaction(); 
    Fragment prev = fmgr.findFragmentByTag(TAG); 
    if (prev != null) { 
     try { 
      ft.remove(prev); 
     } catch (IllegalStateException ex) { 
      // issue: http://code.google.com/p/android/issues/detail?id=17029 
     } 
    } 

    TwoButtonDialogFragment newFragment = new TwoButtonDialogFragment(); 
    if (args == null) { 
     args = new Bundle(); 
    } 
    args.putString("title", title); 
    args.putString("message", msg); 
    args.putString("positiveButtonText", positiveButtonText); 
    args.putString("negativeButtonText", negativeButtonText); 
    args.putInt("id", id); 
    newFragment.setArguments(args); 
    newFragment.setCancelable(false); 
    newFragment.show(fmgr, TAG); // exception is thrown here 
    ft.commit(); 
} 


@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    final Bundle args = getArguments(); 
    String title = args.getString("title"); 
    String msg = args.getString("message"); 
    String positiveButtonText = args.getString("positiveButtonText"); 
    String negativeButtonText = args.getString("negativeButtonText"); 
    final int id = args.getInt("id"); 

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    if (!TextUtils.isEmpty(title)) { 
     builder.setTitle(title); 
    } 
    builder.setMessage(msg); 

    final TwoButtonDialogHandler handler = (TwoButtonDialogHandler) getActivity(); 
    builder.setPositiveButton(positiveButtonText, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int whichButton) {    
      handler.doPositiveClick(id, args); 
     } 
    }); 
    builder.setNegativeButton(negativeButtonText, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int whichButton) { 
      handler.doNegativeClick(id, args); 
     } 
    }); 

    return builder.create(); 
} 

这是ICS上的错误吗?我应该做些什么?

+0

请参阅[这里](http://stackoverflow.com/questions/7992496/how-to-handle-asynctask-onpostexecute-when-paused-to-avoid-illegalstateexception)相似的问题和答案。 – PJL 2011-12-15 15:59:36

回答

3

我遇到了这个问题,并且在框架中找不到解决此问题的方法。

但是我确实提供了一个解决方法,你可以在以下link

2

这谷歌link地址同样的问题,看到问题。看起来像它在兼容性lib中的一个错误。