使用getApplicationContext()显示对话框 - BadTokenException:无法添加窗口 [英] Show dialog using getApplicationContext() - BadTokenException: Unable to add window

查看:338
本文介绍了使用getApplicationContext()显示对话框 - BadTokenException:无法添加窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这可能在此之前已经讨论过。但我仍然困惑,如果我使用会发生什么 getApplicationContext()显示对话框

我有使用 getApplicationContext()以创建 AlertDialog.Builder 应用的应用程序显示在对话框崩溃。但是,如果我用SomeActivity.this情况下正常工作。

注意:这个程序是游戏商店现在一段时间,是工作过,但不知道从哪个Android版本这个崩溃

崩溃堆栈跟踪

  android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌null不是一个应用程序
        在android.view.ViewRootImpl.setView(ViewRootImpl.java:540)
        在android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259)
        在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
        在android.app.Dialog.show(Dialog.java:286)
        在android.app.AlertDialog $ Builder.show(AlertDialog.java:951)
 

解决方案
  

不过,我仍然困惑,如果我使用getApplicationContext()来显示一个对话框,会发生什么情况。

您炸毁。

  

但是,如果我用SomeActivity.this情况下正常工作。

正确的。 A 对话框活动拥有。 不能使用应用程序服务或其他上下文来显示对话框 。但是请注意,你可以使用任何上下文来开始一个对话为主题的活动。

  

但不知道从哪个Android版本这个崩溃

安卓1.0,据我所知。

I know this might have discussed here before. But I am still confused what happens if I use getApplicationContext() to display a Dialog.

I have an application which uses getApplicationContext() to create AlertDialog.Builder, and the app crashes when the dialog is shown. But if I use SomeActivity.this context it works fine.

Note: This app was in play store for a while now and was working before, but not sure from which Android version this crashes.

Crash Stack Trace

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
        at android.view.ViewRootImpl.setView(ViewRootImpl.java:540)
        at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
        at android.app.Dialog.show(Dialog.java:286)
        at android.app.AlertDialog$Builder.show(AlertDialog.java:951) 

解决方案

But I am still confused what happens if I use getApplicationContext() to display a Dialog.

You blow up.

But if I use SomeActivity.this context it works fine.

Correct. A Dialog is owned by an Activity. You cannot use an Application, Service, or other Context to show a Dialog. Note, though, that you can use any Context to start a dialog-themed activity.

but not sure from which Android version this crashes

Android 1.0, as far as I know.

这篇关于使用getApplicationContext()显示对话框 - BadTokenException:无法添加窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆