AsyncTask 中的 ProgressDialog 抛出异常
我试图在我的 AsyncTask
获取数据时显示一个简单的 ProgressDialog
。在我的 onPreExecute() 方法中,我有以下内容:
pd = ProgressDialog.show(c, "Loading...", "Please wait");
c
是从 this.getApplicationContext( 传递到我的
。不幸的是,我不断收到此消息的异常:AsyncTask
构造函数中的上下文)
无法添加窗口 - 令牌 null 不适用于应用程序
我做错了什么?
更新:使用this
而不是this.getApplicationContext()
揭示了另一个问题。当我调用 ProgressDialog.show(...
) 时,会显示 ProgressDialog,但要等到 AsyncTask
完成后才会显示。换句话说,先加载数据,然后再加载对话框如果我在 onPostExecute()
中包含 pd.dismiss()
,那么我什至都看不到该对话框(可能是因为它在打开之前就已关闭)。
最终解决方案: 事实证明,fetch.get()
占用了 UI 线程并且不让 ProgressDialog 显示。
I'm trying to make a simple ProgressDialog
appear while my AsyncTask
is fetching data. In my onPreExecute()
method I have this:
pd = ProgressDialog.show(c, "Loading...", "Please wait");
c
is the context passed into the constructor of my AsyncTask
from this.getApplicationContext()
. Unfortunately, I keep getting an exception with this message:
Unable to add window -- Token null is not for an application
What am I doing wrong?
Update: Using this
instead of this.getApplicationContext()
has revealed another problem. When I call ProgressDialog.show(...
, a ProgressDialog is displayed, but not until after the AsyncTask
has completed. In other words, the data loads and then the dialog is displayed. If I include pd.dismiss()
in my onPostExecute()
then I never even see the dialog (presumable because it is closed before it ever gets opened).
Final solution: It turns out that fetch.get()
was hogging the UI thread and not letting the ProgressDialog display.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个
,是的,我认为你的上下文也有同样的问题。
try this
and yes I think the same the problem is with your context.
使用
YourClassName.this
而不是使用getApplicationContext()
或this.getApplicationContext()
Use
YourClassName.this
instead of usinggetApplicationContext()
orthis.getApplicationContext()