我正在尝试在我的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显示。
答案 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()