Android:ProgressDialog.show() 与 getApplicationContext 崩溃
我似乎无法理解为什么会发生这种情况。这段代码:
mProgressDialog = ProgressDialog.show(this, "", getString(R.string.loading), true);
工作得很好。但是,此代码:
mProgressDialog = ProgressDialog.show(getApplicationContext(), "", getString(R.string.loading), true);
引发以下异常:
W/WindowManager( 569): Attempted to add window with non-application token WindowToken{438bee58 token=null}. Aborting.
D/AndroidRuntime( 2049): Shutting down VM
W/dalvikvm( 2049): threadid=3: thread exiting with uncaught exception (group=0x4001aa28)
E/AndroidRuntime( 2049): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime( 2049): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tastekid.TasteKid/com.tastekid.TasteKid.YouTube}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
E/AndroidRuntime( 2049): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
E/AndroidRuntime( 2049): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
E/AndroidRuntime( 2049): at android.app.ActivityThread.access$2100(ActivityThread.java:116)
E/AndroidRuntime( 2049): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
E/AndroidRuntime( 2049): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 2049): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 2049): at android.app.ActivityThread.main(ActivityThread.java:4203)
E/AndroidRuntime( 2049): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 2049): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 2049): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
E/AndroidRuntime( 2049): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
E/AndroidRuntime( 2049): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 2049): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
E/AndroidRuntime( 2049): at android.view.ViewRoot.setView(ViewRoot.java:460)
E/AndroidRuntime( 2049): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
E/AndroidRuntime( 2049): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
E/AndroidRuntime( 2049): at android.app.Dialog.show(Dialog.java:238)
E/AndroidRuntime( 2049): at android.app.ProgressDialog.show(ProgressDialog.java:107)
E/AndroidRuntime( 2049): at android.app.ProgressDialog.show(ProgressDialog.java:90)
E/AndroidRuntime( 2049): at com.tastekid.TasteKid.YouTube.onCreate(YouTube.java:45)
E/AndroidRuntime( 2049): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
E/AndroidRuntime( 2049): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
E/AndroidRuntime( 2049): ... 11 more
有什么想法为什么会发生这种情况吗?我从 onCreate
方法调用它。
I can't seem to grasp why this is happening. This code:
mProgressDialog = ProgressDialog.show(this, "", getString(R.string.loading), true);
works just fine. However, this code:
mProgressDialog = ProgressDialog.show(getApplicationContext(), "", getString(R.string.loading), true);
throws the following exception:
W/WindowManager( 569): Attempted to add window with non-application token WindowToken{438bee58 token=null}. Aborting.
D/AndroidRuntime( 2049): Shutting down VM
W/dalvikvm( 2049): threadid=3: thread exiting with uncaught exception (group=0x4001aa28)
E/AndroidRuntime( 2049): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime( 2049): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tastekid.TasteKid/com.tastekid.TasteKid.YouTube}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
E/AndroidRuntime( 2049): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
E/AndroidRuntime( 2049): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
E/AndroidRuntime( 2049): at android.app.ActivityThread.access$2100(ActivityThread.java:116)
E/AndroidRuntime( 2049): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
E/AndroidRuntime( 2049): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 2049): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 2049): at android.app.ActivityThread.main(ActivityThread.java:4203)
E/AndroidRuntime( 2049): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 2049): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 2049): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
E/AndroidRuntime( 2049): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
E/AndroidRuntime( 2049): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 2049): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
E/AndroidRuntime( 2049): at android.view.ViewRoot.setView(ViewRoot.java:460)
E/AndroidRuntime( 2049): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
E/AndroidRuntime( 2049): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
E/AndroidRuntime( 2049): at android.app.Dialog.show(Dialog.java:238)
E/AndroidRuntime( 2049): at android.app.ProgressDialog.show(ProgressDialog.java:107)
E/AndroidRuntime( 2049): at android.app.ProgressDialog.show(ProgressDialog.java:90)
E/AndroidRuntime( 2049): at com.tastekid.TasteKid.YouTube.onCreate(YouTube.java:45)
E/AndroidRuntime( 2049): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
E/AndroidRuntime( 2049): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
E/AndroidRuntime( 2049): ... 11 more
Any ideas why this is happening? I'm calling this from the onCreate
method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
尝试 -
Try -
(兼容性)片段也有类似的问题,其中在
ProgressDialog.show()
中使用getActivity()
会导致崩溃。我同意这是因为时机。一个可能的解决方案:
而不是使用
尽早放置 mContext,以便有更多时间来获取上下文。仍然不能保证这会起作用,它只是降低了崩溃的可能性。如果它仍然不起作用,您就必须求助于计时器黑客(这可能会导致其他计时问题,例如稍后关闭对话框)。
当然,如果您可以使用
this
或ActivityName.this
,它会更稳定,因为this
已经指向某些内容。但在某些情况下,比如某些 Fragment 架构,这不是一个选择。Had a similar problem with (compatibility) Fragments in which using a
getActivity()
withinProgressDialog.show()
crashes it. I'd agree that it is because of timing.A possible fix:
instead of using
Place the mContext as early as possible to give it more time to grab the context. There's still no guarantee that this will work, it just reduces the likelihood of a crash. If it still doesn't work, you'd have to resort to the timer hack (which can cause other timing problems like dismissing the dialog later).
Of course, if you can use
this
orActivityName.this
, it's more stable becausethis
already points to something. But in some cases, like with certain Fragment architectures, it's not an option.(供将来参考)
我认为这是因为应用程序上下文和活动上下文存在差异,如下所述: http://www.doubleencore.com/2013/06/context/
这意味着我们无法使用应用程序上下文显示对话框。就是这样。
(For future references)
I think it's because there's differences in Application Context and Activity Context, as explained here: http://www.doubleencore.com/2013/06/context/
Which means that we can't show dialog using Application Context. That's it.
要在 Activity 内使用对话框,请按以下方式操作:
若要在 Fragment 内使用对话框,请按以下方式操作:
就是这样 ^_^
For using dialogs inside activities, do it this way:
For using dialogs inside fragments, do it this way:
That's it ^_^
为了解决这个问题,我所做的就是为我存储全局数据的所有活动创建一个基类。在第一个活动中,我将上下文保存在基类的变量中,如下所示:
基类
第一个活动从基类派生
然后在创建对话框时使用 mycontext 而不是 getApplicationContext。
What I did to get around this was to create a base class for all my activities where I store global data. In the first activity, I saved the context in a variable in my base class like so:
Base Class
First Activity derived from the Base Class
Then I use mycontext instead of getApplicationContext when creating dialogs.
如果您在片段中调用 ProgressDialog.show() ,则将 mContext 转换为 Activity 对我有用。
If you're calling ProgressDialog.show() in a fragment, casting the mContext to Activity worked for me.
这是一个常见问题。
使用
this
而不是getApplicationContext()
这应该可以解决你的问题
This is a common problem.
Use
this
instead ofgetApplicationContext()
That should solve your problem
我已经实现了警报对话框,用于将异常抛出到当前活动视图。每当我给出这样的
给定相同的窗口异常时。我在 onCreate() 之外编写警报代码。如此简单,我使用了
context = this;
context = this; 在onCreate()
方法中的setContentView()
语句之后。将上下文变量视为全局变量,如Context context;
代码示例是
Alert 方法示例是
它对我来说效果很好。实际上,我在 StackOverflow 上搜索了这个错误,我发现了这个查询。在阅读了这篇文章的所有回复后,我尝试了这种方式,所以它有效。我认为这是克服异常的简单解决方案。
谢谢,
拉詹达尔
I have implemented Alert Dialog for exception throwing on to the current activitty view.Whenever I had given like this
Given same Window Exception.I write code for alert out of onCreate().So simple I used
context = this;
aftersetContentView()
statement inonCreate()
method.Taken context variable as global likeContext context;
Code sample is
Alert Method Sample is
It works fine for me.Actually I searched for this error on StackOverflow I found this query.After reading all responses of this post, I tried this way so It works .I thought this is a simple solution for overcome the exception.
Thanks,
Rajendar
如果您在 groupActivity 上遇到问题,请不要使用它。
PARENT 是来自 Parent ActivityGroup 的静态值。
而不是
if you have a problem on groupActivity dont use this.
PARENT is a static from the Parent ActivityGroup.
instead of
对话框始终作为活动的一部分创建和显示。您需要传入 Activity 上下文而不是应用程序上下文。
http://developer.android.com/guide/topics/ui/dialogs .html#ShowingADialog
A dialog is always created and displayed as a part of an Activity. You need to pass in an Activity context instead of the Application context.
http://developer.android.com/guide/topics/ui/dialogs.html#ShowingADialog
我使用的是 Android 版本 2.1 和 API 级别 7。我遇到了这个(或类似的)问题,并通过使用以下方法解决:
而不是这个:
I am using Android version 2.1 with API Level 7. I faced with this (or similar) problem and solved by using this:
instead of this:
对我来说,改变工作
很
奇怪,第一个可以在谷歌教程中找到,人们在这方面遇到错误。
For me worked changing
to
Weird thing is that the first one can be found in google tutorial and people get error on this..
您使用的是哪个 API 版本?如果我对问题所在的理解是正确的,那么这个问题已在 Android 1.6(API 版本 4)中得到修复。
看起来 getApplicationContext() 返回的对象引用只是指向 null。我认为您遇到的问题与我遇到的问题类似,即
onCreate()
中的一些代码在窗口实际构建完成之前运行。这将是一个黑客,但尝试在几百毫秒内启动一个新线程(IIRC:300-400似乎对我有用,但你需要修补),打开你的 ProgressDialog 并启动你需要的任何其他内容(例如网络IO)。像这样的东西:Which API version are you using? If I'm right about what the problem is then this was fixed in Android 1.6 (API version 4).
It looks like the object reference that
getApplicationContext()
is returning just points to null. I think you're having a problem similar to one I had in that some of the code in theonCreate()
is being run before the window is actually done being built. This is going to be a hack, but try launching a new Thread in a few hundred milliseconds (IIRC: 300-400 seemed to work for me, but you'll need to tinker) that opens your ProgressDialog and starts anything else you needed (eg. network IO). Something like this:我不认为这是围绕空应用程序上下文的计时问题
尝试在您的应用程序中扩展应用程序(或者如果您已经有,则直接使用它)
使实例可用作私有单例。这是never null
在MyApp中创建一个静态助手(它将使用单例)
BOOM!!
此外,请在此处查看android工程师的答案:WindowManager$BadTokenException
现在,我同意,该方法采用 Context 参数而不是 Activity 是没有意义的。
I don't think this is a timing issue around a null application context
Try extending Application within your app (or just use it if you already have)
Make the instance available as a private singleton. This is never null
Make a static helper in MyApp (which will use the singleton)
BOOM!!
Also, check out android engineer's answer here: WindowManager$BadTokenException
Now, i agree, it does not make sense that the method takes a Context param, instead of Activity..
阅读上述答案后,我发现对于我的情况,以下解决了问题。
这引发了错误
基于之前的答案表明上下文是错误的,我更改了 getApplicationContext() 以从传递到按钮 onClick 方法的视图中检索上下文。
我不完全理解Java的工作原理,所以我可能是错的,但我猜测对于我的具体情况,原因可能与上面的代码片段是在抽象活动类中定义的事实有关;被许多 Activity 继承和使用,也许这导致了 getApplicationContext() 不返回有效上下文的事实? (只是猜测)。
Having read the above answers i found that for my situation the following fixed the issue.
This threw the error
Based on the previous answers that suggested the context was the wrong one, i changed the getApplicationContext() to retrieve the context from the View passed in to the buttons onClick method.
I don't fully understand the workings of Java so i could be wrong, but I'm guessing that for my specific situation the cause could have been related to the fact that the above snippet was defined in an Abstract Activity class; inherited and used by many Activities, perhaps that contributed to the fact that getApplicationContext() doesn't return a valid context?? (Just a guess).
我正在创建一个带有逐项叠加的地图视图。我正在从我的mapActivity创建我的itemizedoverlay:
我发现当我的itemizedoverlay的onTap方法被触发时,我会得到“android.view.WindowManager$BadTokenException:无法添加窗口——令牌null不适用于应用程序”异常(当在地图视图上点击该位置时)。
我发现如果我只是将“this”而不是“getApplicationContext()”传递给我的构造函数,问题就消失了。这似乎支持了alienjazzcat的结论。诡异的。
I am creating a map view with itemized overlays. I was creating my itemizedoverlay like this from my mapActivity:
I found that I would get the "android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application" exception when my itemizedoverlay's onTap method was triggered(when the location is tapped on the mapview).
I found that if I simply passed, 'this' instead of 'getApplicationContext()' to my constructor, the problem went away. This seems to support alienjazzcat's conclusion. weird.
对于 TabActivities 中显示的活动,请使用 getParent()
而不是
For Activities shown within TabActivities use getParent()
instead of
适用于 Android 2.2
使用此代码:
而不是此代码:
备注:我的自定义对话框是在
activity.onCreateDialog(int dialId)
方法之外创建的。For Android 2.2
Use this code:
instead of this code:
Remark: My custom dialog is created outside
activity.onCreateDialog(int dialogId)
method.