当前位置: 首页 > 面试题库 >

IllegalStateException:在具有onActivityResult的onSaveInstanceState之后无法执行此操作

欧阳俊晖
2023-03-14
问题内容

我是Android开发的新手。我有一个问题。我尝试了最后几个小时,但我无法弄清楚。如果是这样,我有一个很普遍的问题。IllegalStateException:使用ViewPager在onSaveInstanceState之后无法执行此操作,但由于缺少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");  
          } 
       }
   }

这是错误:

位于android.os.Handler.dispatchMessage(Handler.java:102)的ActivityThread $
H.handleMessage(ActivityThread.java:1352)位于android.app.ActivityThread.main的android.os.Looper.loop(Looper.java:135)
(ActivityThread.java:5254)at java.lang.reflect.Method.invoke(本地方法)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报告上传完成:5C2EEA4F018E-0001-3529-63E978D09744 01-04 05:08:59.955
13609-13609 / com.nazmul.aznazgame.bitlife I / Process:正在发送信号。PID:13609
SIG:9
1352)在android.os.Handler.dispatchMessage(Handler.java:102)在android.os.Looper.loop(Looper.java:135)在android.app.ActivityThread.main(ActivityThread.java:5254)在java。
com.android.internal.os.ZygoteInit $
MethodAndArgsCaller.run(ZygoteInit.java:903)处java.lang.reflect.Method.invoke(Method.java:372)处的lang.reflect.Method.invoke(本机方法)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)01-04
05:08:59.825 13609-13651 / com.nazmul.aznazgame.bitlife I /
CrashlyticsCore:Crashlytics报告上传完成:5C2EEA4F018E-0001- 3529-63E978D09744 01-04
05:08:59.955 13609-13609 / com.nazmul.aznazgame.bitlife I /
Process:正在发送信号。PID:13609 SIG:9
1352)在android.os.Handler.dispatchMessage(Handler.java:102)在android.os.Looper.loop(Looper.java:135)在android.app.ActivityThread.main(ActivityThread.java:5254)在java。
com.android.internal.os.ZygoteInit $
MethodAndArgsCaller.run(ZygoteInit.java:903)处java.lang.reflect.Method.invoke(Method.java:372)处的lang.reflect.Method.invoke(本机方法)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)01-04
05:08:59.825 13609-13651 / com.nazmul.aznazgame.bitlife I /
CrashlyticsCore:Crashlytics报告上传完成:5C2EEA4F018E-0001- 3529-63E978D09744 01-04
05:08:59.955 13609-13609 / com.nazmul.aznazgame.bitlife I /
Process:正在发送信号。PID:13609 SIG:9
在java.lang.reflect.Method.invoke(本机方法)在android.app.ActivityThread.main(ActivityThread.java:5254)处循环(Looper.java:135)在java.lang.reflect.Method.invoke(Method。
java:372)在com.android.internal.os.ZygoteInit $
MethodAndArgsCaller.run(ZygoteInit.java:903)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)01-04
05:08 :59.825 13609-13651 / com.nazmul.aznazgame.bitlife I /
CrashlyticsCore:Crashlytics报告上传完成:5C2EEA4F018E-0001-3529-63E978D09744 01-04
05:08:59.955 13609-13609 / com.nazmul.aznazgame.bitlife I /处理:发送信号。PID:13609
SIG:9
在java.lang.reflect.Method.invoke(本机方法)在android.app.ActivityThread.main(ActivityThread.java:5254)处循环(Looper.java:135)在java.lang.reflect.Method.invoke(Method。
java:372)在com.android.internal.os.ZygoteInit $
MethodAndArgsCaller.run(ZygoteInit.java:903)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)01-04
05:08 :59.825 13609-13651 / com.nazmul.aznazgame.bitlife I /
CrashlyticsCore:Crashlytics报告上传完成:5C2EEA4F018E-0001-3529-63E978D09744 01-04
05:08:59.955 13609-13609 / com.nazmul.aznazgame.bitlife I /处理:发送信号。PID:13609
SIG:9 java:372)在com.android.internal.os.ZygoteInit $
MethodAndArgsCaller.run(ZygoteInit.java:903)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)01-04
05:08 :59.825 13609-13651 / com.nazmul.aznazgame.bitlife I /
CrashlyticsCore:Crashlytics报告上传完成:5C2EEA4F018E-0001-3529-63E978D09744 01-04
05:08:59.955 13609-13609 / com.nazmul.aznazgame.bitlife I /处理:发送信号。PID:13609
SIG:9 java:372)在com.android.internal.os.ZygoteInit $
MethodAndArgsCaller.run(ZygoteInit.java:903)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)01-04
05:08 :59.825 13609-13651 / com.nazmul.aznazgame.bitlife I /
CrashlyticsCore:Crashlytics报告上传完成:5C2EEA4F018E-0001-3529-63E978D09744 01-04
05:08:59.955 13609-13609 / com.nazmul.aznazgame.bitlife I /处理:发送信号。PID:13609
SIG:9 正在发送信号。PID:13609 SIG:9 正在发送信号。PID:13609 SIG:9


问题答案:

您必须super.onActivityResult(requestCode, resultCode, data)onActivityResult()方法中执行任何FragmentTransactions之前先调用,因为该调用是“解锁”
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");  
          } 
       }
   }


 类似资料: