Android IllegalStateException - 片段支持库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11900785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:07:36  来源:igfitidea点击:

IllegalStateException - Fragment support library

androidillegalstateexception

提问by phlebas

I have a serious problem with my App continously crashing on some users devices with the following exception in an Activity onStart method:

我有一个严重的问题,我的应用程序在某些用户设备上不断崩溃,但在 Activity onStart 方法中出现以下异常:

Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1299)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1310)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:541)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:525)
at android.support.v4.app.DialogFragment.show(DialogFragment.java:123) 

I cannot reproduce this error locally in my emulator. I have found a few questions regarding this issue on stackoverflow: hereand here

我无法在模拟器中本地重现此错误。我在stackoverflow上发现了一些关于这个问题的问题: herehere

I have tried the suggested solution, i.e. do not allow empty bundles in onSaveInstance neither in fragments nor activities.

我已经尝试了建议的解决方案,即不允许在 onSaveInstance 中出现空包,无论是片段还是活动。

However the problem remains. This is the only Crash report I ever get, and I get it continously.

然而问题依然存在。这是我收到的唯一一次崩溃报告,而且我一直在收到。

Does anyone have a working solution?

有没有人有一个可行的解决方案?

采纳答案by phlebas

I have solved this issue by

我已经解决了这个问题

Check my answer here for code: "Can not perform this action after onSaveInstanceState" - why am I getting this exception from my activity's onResume method?

在此处查看代码的答案:“在 onSaveInstanceState 后无法执行此操作”-为什么我会从活动的 onResume 方法中收到此异常?

Got no more IllegalStateException so far.

到目前为止没有更多的 IllegalStateException。

回答by PearsonArtPhoto

I solved this using this workaround, namely put the code into a methods onPostResume()function in the Activity

我使用此解决方法解决了这个问题,即将代码放入onPostResume()活动中的方法函数中

回答by Tatarize

Read This Article.

阅读本文。

And,

和,

This one.

这个。

Once the issue is understood, go ahead and change things properly:

了解问题后,继续并正确更改:

Figure out why your transactions are happening outside your onResume state. Why are these things happening when they shouldn't be. Why are you manually adding and removing fragments with regard to the lifecycle of you app rather than when the user requests. You should maybe need one time in the onCreate() or when the user invokes something. But why are you messing with them in onResume() in the first place? Don't do that.

弄清楚为什么您的交易发生在您的 onResume 状态之外。为什么这些事情在不应该发生的时候发生。为什么要根据应用程序的生命周期而不是在用户请求时手动添加和删除片段。您可能需要在 onCreate() 中或在用户调用某些内容时需要一次。但是为什么你首先要在 onResume() 中弄乱它们呢?不要那样做。

If you change all the .commit()to .commitAllowStateLoss()then the crashes will go away. But, you're better off not doing that. You are better off making sure you never change your fragments except when the app is fully live. Sometimes that means never loading a fragment from the OnActivityResult() but rather flagging to run in your onPostResume(), and avoiding performing UI changes in async threads that wrongly assume you couldn't have killed the activity in the meantime.

如果您将所有更改为.commit().commitAllowStateLoss()那么崩溃将消失。但是,你最好不要这样做。你最好确保你永远不会改变你的片段,除非应用程序完全上线。有时这意味着永远不要从 OnActivityResult() 加载片段,而是标记在 onPostResume() 中运行,并避免在异步线程中执行 UI 更改,这些更改错误地认为您无法在此期间终止活动。

If you just want to spackle everything, .commitAllowStateLoss()will do that. If you want to do it right, make sure you don't fiddle with your fragments after things die.

如果你只是想抹掉所有东西,.commitAllowStateLoss()那就去做吧。如果你想把它做对,确保你在事情死后不要摆弄你的碎片。

One of the more recent forms of Android circa Oreo I think, changed this to avoid this annoying error. My answer seems a bit vague but it's because the bug is in the paradigm. You entirely can hit that with various code elements and fairly routinely.

我认为大约是 Oreo 的最新形式的 Android 之一改变了这一点以避免这个烦人的错误。我的回答似乎有点含糊,但这是因为错误在范式中。您完全可以使用各种代码元素并且相当常规地实现它。