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

时间:2019-01-04 05:28:31

标签: java android android-fragments android-fragmentactivity android-dialogfragment

我是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:关闭VM 01-04 05:08:57.068   13609-13609 / com.nazmul.aznazgame.bitlife E / Android运行时:致命   例外:主要       流程:com.nazmul.aznazgame.bitlife,PID:13609       java.lang.RuntimeException:无法交付结果ResultInfo {who = null,request = 4,result = -1,data = Intent {(有附加功能)   }}到活动   {com.nazmul.aznazgame.bitlife / com.nazmul.aznazgame.bitlife.MainActivity}:   java.lang.IllegalStateException:之后无法执行此操作   onSaveInstanceState           在android.app.ActivityThread.deliverResults(ActivityThread.java:3574)           在android.app.ActivityThread.handleSendResult(ActivityThread.java:3617)           在android.app.ActivityThread.access $ 1300(ActivityThread.java:151)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1352)           在android.os.Handler.dispatchMessage(Handler.java:102)           在android.os.Looper.loop(Looper.java:135)           在android.app.ActivityThread.main(ActivityThread.java:5254)           在java.lang.reflect.Method.invoke(本机方法)           在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)        原因:java.lang.IllegalStateException:onSaveInstanceState之后无法执行此操作           在android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:2053)           在android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:2079)           在android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:678)           在android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:632)           在android.support.v4.app.DialogFragment.show(DialogFragment.java:143)           在com.nazmul.aznazgame.bitlife.MainActivity.showEducationDialog(MainActivity.java:1716)           在com.nazmul.aznazgame.bitlife.MainActivity.manageActivity(MainActivity.java:604)           在com.nazmul.aznazgame.bitlife.MainActivity.onActivityResult(MainActivity.java:580)           在android.app.Activity.dispatchActivityResult(Activity.java:6192)           在android.app.ActivityThread.deliverResults(ActivityThread.java:3570)           在android.app.ActivityThread.handleSendResult(ActivityThread.java:3617)           在android.app.ActivityThread.access $ 1300(ActivityThread.java:151)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1352)           在android.os.Handler.dispatchMessage(Handler.java:102)           在android.os.Looper.loop(Looper.java:135)           在android.app.ActivityThread.main(ActivityThread.java:5254)           在java.lang.reflect.Method.invoke(本机方法)           在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 / Process:正在发送信号。   PID:13609 SIG:9

2 个答案:

答案 0 :(得分:2)

在您的<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <p>not able to drop correctly on the rectangle:</p> <div class="container2"> <div class="box" id="box1" draggable="true" ondragstart="drag(event)" style="background-image:url(https://picsum.photos/200/300)"> <p name="values" id="name1" class="hidden"></p> </div> <div class="box" id="box2" draggable="true" ondragstart="drag(event)" style="background-image:url(https://picsum.photos/g/200/300)"> <p name="values" id="name2" class="hidden"></p> </div> <div class="box" id="box3" draggable="true" ondragstart="drag(event)" style="background-image:url(https://picsum.photos/200/300?image=0)"> <p name="values" id="name3" class="hidden"></p> </div> <div class="box" id="box4" draggable="true" ondragstart="drag(event)" style="background-image:url(https://picsum.photos/200/300/?gravity=east)"> <p name="values" id="name4" class="hidden"></p> </div> </div> <div class="boxright1" ondrop="drop(event)" ondragover="allowDrop(event)" id="4" name="d"></div>方法中执行任何FragmentTransactions之前,您必须先调用super.onActivityResult(requestCode, resultCode, data),因为该调用可以“解锁” FragmentManager,并指出您处于有效状态才能执行FragmentTransactions。

onActivityResult()

答案 1 :(得分:0)

这是一个常见问题。您有2种方法来处理此问题。

  1. 您可以覆盖show()方法并将其内容放在try-catch块中
  2. 您可以按照以下方式显示对话框

     FragmentManager fm = getSupportFragmentManager();
     MyDialog d = new MyDialog();
     FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
     ft.add(d, "dialog");
     ft.commitAllowingStateLoss();