IllegalStateException:在使用 onActivityResult 的 onSaveInstanceState 之后无法执行此操作
IllegalStateException: Can not perform this action after onSaveInstanceState with onActivityResult
我是 Android 开发的新手。我有一个问题。我在最后几个小时尝试过,但我无法弄清楚这一点。如果是这样,我有一个受欢迎的问题。 IllegalStateException: Can not perform this action after onSaveInstanceState with ViewPager 但由于缺乏 Android 开发经验而失败。
代码如下:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_OK) {
MyCustomDialogFragment newPopup = new MyCustomDialogFragment();
newPopup.setMyClickListener(MainActivity.this);
FragmentManager fragmentManager = getSupportFragmentManager();
newPopup.show(fragmentManager, "CashReceivePopup");
}
}
}
这里是错误:
01-04 05:08:57.010 13609-13609/com.nazmul.aznazgame.bitlife
D/AndroidRuntime: Shutting down VM 01-04 05:08:57.068
13609-13609/com.nazmul.aznazgame.bitlife E/AndroidRuntime: FATAL
EXCEPTION: main
Process: com.nazmul.aznazgame.bitlife, PID: 13609
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=4, result=-1, data=Intent { (has extras)
}} to activity
{com.nazmul.aznazgame.bitlife/com.nazmul.aznazgame.bitlife.MainActivity}:
java.lang.IllegalStateException: Can not perform this action after
onSaveInstanceState
at android.app.ActivityThread.deliverResults(ActivityThread.java:3574)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3617)
at android.app.ActivityThread.access00(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1352)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:2053)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:2079)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:678)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:632)
at android.support.v4.app.DialogFragment.show(DialogFragment.java:143)
at com.nazmul.aznazgame.bitlife.MainActivity.showEducationDialog(MainActivity.java:1716)
at com.nazmul.aznazgame.bitlife.MainActivity.manageActivity(MainActivity.java:604)
at com.nazmul.aznazgame.bitlife.MainActivity.onActivityResult(MainActivity.java:580)
at android.app.Activity.dispatchActivityResult(Activity.java:6192)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3570)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3617)
at android.app.ActivityThread.access00(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1352)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 01-04
05:08:59.825 13609-13651/com.nazmul.aznazgame.bitlife
I/CrashlyticsCore: Crashlytics report upload complete:
5C2EEA4F018E-0001-3529-63E978D09744 01-04 05:08:59.955
13609-13609/com.nazmul.aznazgame.bitlife I/Process: Sending signal.
PID: 13609 SIG: 9
在您的 onActivityResult()
方法中执行任何 FragmentTransactions 之前,您必须先调用 super.onActivityResult(requestCode, resultCode, data)
,因为该调用是 'unlocks' FragmentManager 并指出您处于执行 FragmentTransactions 的有效状态。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Add this line
super.onActivityResult(requestCode, resultCode, data);
// This all remains the same
if (requestCode == 1) {
if(resultCode == RESULT_OK) {
MyCustomDialogFragment newPopup = new MyCustomDialogFragment();
newPopup.setMyClickListener(MainActivity.this);
FragmentManager fragmentManager = getSupportFragmentManager();
newPopup.show(fragmentManager, "CashReceivePopup");
}
}
}
这是一个常见问题。您有 2 个选项来处理这个问题。
- 您可以覆盖
show()
方法并将其内容放入 try-catch
块中
您按以下方式显示对话框
FragmentManager fm = getSupportFragmentManager();
MyDialog d = new MyDialog();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(d, "dialog");
ft.commitAllowingStateLoss();
我是 Android 开发的新手。我有一个问题。我在最后几个小时尝试过,但我无法弄清楚这一点。如果是这样,我有一个受欢迎的问题。 IllegalStateException: Can not perform this action after onSaveInstanceState with ViewPager 但由于缺乏 Android 开发经验而失败。
代码如下:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_OK) {
MyCustomDialogFragment newPopup = new MyCustomDialogFragment();
newPopup.setMyClickListener(MainActivity.this);
FragmentManager fragmentManager = getSupportFragmentManager();
newPopup.show(fragmentManager, "CashReceivePopup");
}
}
}
这里是错误:
01-04 05:08:57.010 13609-13609/com.nazmul.aznazgame.bitlife D/AndroidRuntime: Shutting down VM 01-04 05:08:57.068 13609-13609/com.nazmul.aznazgame.bitlife E/AndroidRuntime: FATAL EXCEPTION: main Process: com.nazmul.aznazgame.bitlife, PID: 13609 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=4, result=-1, data=Intent { (has extras) }} to activity {com.nazmul.aznazgame.bitlife/com.nazmul.aznazgame.bitlife.MainActivity}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState at android.app.ActivityThread.deliverResults(ActivityThread.java:3574) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3617) at android.app.ActivityThread.access00(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1352) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:2053) at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:2079) at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:678) at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:632) at android.support.v4.app.DialogFragment.show(DialogFragment.java:143) at com.nazmul.aznazgame.bitlife.MainActivity.showEducationDialog(MainActivity.java:1716) at com.nazmul.aznazgame.bitlife.MainActivity.manageActivity(MainActivity.java:604) at com.nazmul.aznazgame.bitlife.MainActivity.onActivityResult(MainActivity.java:580) at android.app.Activity.dispatchActivityResult(Activity.java:6192) at android.app.ActivityThread.deliverResults(ActivityThread.java:3570) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3617) at android.app.ActivityThread.access00(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1352) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 01-04 05:08:59.825 13609-13651/com.nazmul.aznazgame.bitlife I/CrashlyticsCore: Crashlytics report upload complete: 5C2EEA4F018E-0001-3529-63E978D09744 01-04 05:08:59.955 13609-13609/com.nazmul.aznazgame.bitlife I/Process: Sending signal. PID: 13609 SIG: 9
在您的 onActivityResult()
方法中执行任何 FragmentTransactions 之前,您必须先调用 super.onActivityResult(requestCode, resultCode, data)
,因为该调用是 'unlocks' FragmentManager 并指出您处于执行 FragmentTransactions 的有效状态。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Add this line
super.onActivityResult(requestCode, resultCode, data);
// This all remains the same
if (requestCode == 1) {
if(resultCode == RESULT_OK) {
MyCustomDialogFragment newPopup = new MyCustomDialogFragment();
newPopup.setMyClickListener(MainActivity.this);
FragmentManager fragmentManager = getSupportFragmentManager();
newPopup.show(fragmentManager, "CashReceivePopup");
}
}
}
这是一个常见问题。您有 2 个选项来处理这个问题。
- 您可以覆盖
show()
方法并将其内容放入try-catch
块中 您按以下方式显示对话框
FragmentManager fm = getSupportFragmentManager(); MyDialog d = new MyDialog(); FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.add(d, "dialog"); ft.commitAllowingStateLoss();