2014-10-28 48 views
0

當我更改屏幕方向時,出現「java.lang.IllegalStateException:onSaveInstanceState後無法執行此操作」,並單擊Image並嘗試打開對話框。我見過超類DialogFragment.show()執行TransactionManager.commit(),然後崩潰。它只發生在我改變方向時。 我不知道如何避免這個例外。 代碼:java.lang.IllegalStateException:在更改屏幕方向後打開對話框

public class CustomView extends LinearLayout { 
    .... 
    final ImageView pic=(ImageView) v.findViewById(R.id.mypic); 
    student_pic.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      final MyDialog  
        picker=MyDialog.newInstance(CustomView.this); 
      picker.show(getContext()); 

     } 
    }); 
} 

MyDialog.java:

public class MyDialog extends DialogFragment { 
    public static MyDialog newInstance(View f) { 
     MyDialog frag = new MyDialog(); 
     return frag; 
    } 
    ..... 
    public void show(Context context) 
    { 
     FragmentManager fm=((Activity)context).getFragmentManager();  
     show(fm, "MyDialog"); // The superclass show() does a FragmentManager.commit() and crash 
    } 
} 

堆棧跟蹤:

10-29 00:05:07.452  
621-621/com.myapp.example E/AndroidRuntime﹕ FATAL EXCEPTION:  main 
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState 
    at android.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1280) 
    at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1291) 
    at android.app.BackStackRecord.commitInternal(BackStackRecord.java:548) 
    at android.app.BackStackRecord.commit(BackStackRecord.java:532) 
    at android.app.DialogFragment.show(DialogFragment.java:230) 
    at com.myapp.table.MyDialog.show(MyDialog.java:234) 
    at com.myapp.table.MyDialog.show(MyDialog.java:224) 
    at com.myapp.table.MyDialog.show(MyDialog.java:217) 
    at com.myapp.table.views.MyCustomView$2$1$1.run(MyCustomView.java:152) 
    at android.os.Handler.handleCallback(Handler.java:615) 
    at android.os.Handler.dispatchMessage(Handler.java:92) 
    at android.os.Looper.loop(Looper.java:137) 
    at android.app.ActivityThread.main(ActivityThread.java:4745) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:511) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
    at dalvik.system.NativeStart.main(Native Method) 

任何幫助,以避免此異常?

+0

發佈stacktrace;) – mithrop 2014-10-28 16:38:33

回答

0

解決方案:不使用DialogFragment,而是使用AlertDialog構建對話框。

相關問題