ProgressDialog in AsyncTask throws an exception

I am trying to make a simple ProgressDialog while my AsyncTask is AsyncTask data. In my onPreExecute() method, I have this:

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

c is the context passed to the constructor of my AsyncTask from this.getApplicationContext() . Unfortunately, I keep getting an exception from this post:

Unable to add window - zero token is not for application

What am I doing wrong?

Update: Using this instead of this.getApplicationContext() revealed yet another problem. When I call ProgressDialog.show(... , the ProgressDialog indicator is displayed, but only after AsyncTask completed. In other words, the data is loaded and then a dialog is displayed. If I include pd.dismiss() in my onPostExecute() , then I even I will not see the dialogue (presumably because it is closed before it ever opens).

Final Solution: It turns out that fetch.get() distorts the UI thread and does not allow the display of ProgressDialog.

+6
android android-asynctask progressdialog
source share
3 answers
 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(); } 
+4
source share

try it

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

and yes, I think the problem is related to your context.

+1
source share

Use YourClassName.this instead of using getApplicationContext() or this.getApplicationContext()

0
source share

All Articles