DialogFragment的BadTokenException onStart

时间:2018-10-24 11:34:40

标签: android android-dialogfragment

我浏览了100篇有关BadTokenException的帖子,但似乎每次都有另一个原因说明这种情况的发生,在我的情况下,我看到此堆栈跟踪了很多次(并不总是发生/不知道如何重现) :

Caused by android.view.WindowManager$BadTokenException: Unable to add window – token android.os.BinderProxy@2a5a564 is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:602)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:322)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:98)
at android.app.Dialog.show(Dialog.java:319)
at android.support.v4.app.DialogFragment.onStart(SourceFile:434)
at com.mycompany.myapp.ui.about.WhatsNewDialog.onStart(SourceFile:89)
at android.support.v4.app.Fragment.performStart(SourceFile:2372)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1467)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(SourceFile:1759)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1827)
at android.support.v4.app.BackStackRecord.executeOps(SourceFile:797)
at android.support.v4.app.FragmentManagerImpl.executeOps(SourceFile:2596)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(SourceFile:2383)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(SourceFile:2338)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(SourceFile:2245)
at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(SourceFile:3248)
at android.support.v4.app.FragmentManagerImpl.dispatchResume(SourceFile:3212)
at android.support.v4.app.FragmentController.dispatchResume(SourceFile:217)
at android.support.v4.app.FragmentActivity.onResumeFragments(SourceFile:509)
at android.support.v4.app.FragmentActivity.onPostResume(SourceFile:498)
at android.support.v7.app.AppCompatActivity.onPostResume(SourceFile:171)
at android.app.Activity.performResume(Activity.java:6415)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3146)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3268)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
at android.app.ActivityThread.access$900(ActivityThread.java:179)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5560)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)

据我了解,跟踪异常直接发生在DialogFragment的onStart上,这是所显示的应用程序本身的唯一代码行,在该行仅调用了super.onStart(),在此代码中不存在任何其他内容这种方法,这真的很令人困惑,您了解吗? 在这种情况下,是否还会使用“ activity.isFinishing()”解决方案?在stacktrace中,我什至没有看到我调用new WhatsNewDialog()。show()的行,因此在以某种方式恢复Dialog的同时,这可能确实在发生,欢迎任何提示。

1 个答案:

答案 0 :(得分:0)

它实际上给你错误。

您正在从非活动活动中启动DialogFragment。因此,要么在启动dialogFragment之后立即完成该活动,要么在活动的死版本或内存泄漏版本中启动它。

如果您需要更多帮助,那么我需要查看从何处启动它,以及下面的代码以查看是否要销毁父级。