AsyncTask中的ProgressDialog抛出异常

时间:2010-06-25 21:02:09

标签: android android-asynctask progressdialog

我正在尝试在我的ProgressDialog获取数据时显示一个简单的AsyncTask。在我的onPreExecute()方法中,我有这个:

pd = ProgressDialog.show(c, "Loading...", "Please wait");

c是从AsyncTask传递到this.getApplicationContext()的构造函数的上下文。不幸的是,我不断收到此消息的异常:

  

无法添加窗口 - 令牌null不适用于应用程序

我做错了什么?

更新:使用this代替this.getApplicationContext()发现了另一个问题。当我调用ProgressDialog.show(...时,会显示ProgressDialog,但直到AsyncTask完成后才会显示。换句话说,数据加载然后显示对话框。如果我在pd.dismiss()中加入onPostExecute(),那么我甚至都看不到对话框(因为它在打开之前就已关闭)。

最终解决方案:事实证明fetch.get()正在占用UI线程而不让ProgressDialog显示。

3 个答案:

答案 0 :(得分:4)

ProgressDialog dialog;
@Override
    protected void onPreExecute() {
        dialog = new ProgressDialog(viewContacts.this);
        dialog.setMessage(getString(R.string.please_wait_while_loading));
        dialog.setIndeterminate(true);
        dialog.setCancelable(false);
        dialog.show();
    }

答案 1 :(得分:1)

试试这个

  this.pd = ProgressDialog.show(this,"Loading...", "Please wait", true, false);

是的,我认为问题与你的背景相同。

答案 2 :(得分:0)

使用YourClassName.this代替getApplicationContext()this.getApplicationContext()