Android:DialogFragment show在Web服务调用后抛出illegalstateexception

时间:2017-07-07 07:23:11

标签: android android-dialogfragment

在我的应用程序中,我在收到Web服务的响应后显示了一个对话框片段。我从var pat = @"(today|tomorrow|yesterday)((?:\s*[+-]?\s*\d+\s*[dwym])*)"; // Can not change Match match = Regex.Match(value, pat); 调用此服务。如果应用程序转到后台,它会崩溃并带有异常

onCreate

我经历了this blog post。仍然有疑问,我应该从哪里调用Fatal Exception: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1493) at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1511) at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:638) at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:617) at android.support.v4.app.DialogFragment.show(DialogFragment.java:139) at com.activity.MainActivity.showAcceptancePopup(MainActivity.java:1655) at com.activity.MainActivity.access$1000(MainActivity.java:164) at com.activity.MainActivity$28.onResponse(MainActivity.java:1568) at com.activity.MainActivity$28.onResponse(MainActivity.java:1547) at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65) at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6123) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757) ,因为它取决于服务器提供的数据。

或者我应该将API调用从dialogFragment.show()移到onCreate

1 个答案:

答案 0 :(得分:1)

此处的解决方案是使用commitAllowingStateLoss();代替commit()进行片段交易,正如documentation所述

  

与commit()类似,但允许在活动之后执行提交   状态已保存

希望这有帮助。