6

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

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

c是传递给 my AsyncTaskfrom的构造函数的上下文this.getApplicationContext()。不幸的是,我不断收到这条消息的异常:

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

我究竟做错了什么?

更新:使用this而不是this.getApplicationContext()揭示了另一个问题。当我调用时ProgressDialog.show(...,会显示一个 ProgressDialog,但直到AsyncTask完成后才会显示。换句话说,数据加载,然后显示对话框。如果我包含pd.dismiss()在我的onPostExecute()then 我什至看不到对话框(可能是因为它在打开之前就关闭了)。

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

4

3 回答 3

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();
    }
于 2010-06-25T21:32:04.487 回答
1

试试这个

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

是的,我认为您的上下文也存在同样的问题。

于 2010-06-25T22:40:11.010 回答
0

使用YourClassName.this而不是使用getApplicationContext()this.getApplicationContext()

于 2015-08-13T16:05:00.907 回答