9

Sau khi API Fragments được phát hành, tôi bắt đầu chuyển tất cả các hộp thoại không được dùng vào DialogFraments bằng gói tương thích. Mọi thứ đã được làm việc tốt, cho đến khi tôi nhận thấy rằng các hộp thoại của tôi đang gây ra tai nạn để chỉ ICS:Hiển thị DialogFragments bị lỗi 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) 

hộp thoại của tôi được hiển thị trên AsyncTask.onPostExecute() để hiển thị một phản ứng http cho người dùng. Sau khi đi sâu vào vấn đề, tôi nhận được kết luận rằng ngoại lệ này chỉ xảy ra khi Hoạt động bị tạm dừng hoặc dừng lại và điều đó không xảy ra trên các phiên bản Android khác. Tôi đã thử sử dụng commitAllowingStateLoss(), nhưng nó không giúp được gì, vì ngoại lệ được ném trên DialogFragment.show(). Đây là mã của tôi cho 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(); 
} 

Đây có phải là lỗi trên ICS không? Tôi phải làm gì bây giờ?

+0

Xem [tại đây] (http://stackoverflow.com/questions/7992496/how-to-handle-asynctask-onpostexecute-when-paused-to-avoid-illegalstateexception) cho câu hỏi và câu trả lời tương tự. – PJL

Trả lời

3

Tôi gặp sự cố này và không thể tìm thấy cách nào trong khung công tác để giải quyết vấn đề này.

Tuy nhiên tôi đã cung cấp một workaround cho vấn đề mà bạn có thể thấy ở phía sau link

2

link địa chỉ cùng một vấn đề của Google này. Dường như một lỗi trong lib tương thích.

Các vấn đề liên quan