ProgressDialog in AsyncTask throws an exception
Asked Answered
H

3

6

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.

Hemia answered 25/6, 2010 at 21:2 Comment(2)
This question has been answered #1562303Darwindarwinian
Ally - Thanks, but I'm not sure its the exactly the same issue. The accepted solution is about an Android bug that was supposedly fixed in 1.6 (I'm using 2.1). I did find another solution, which is to create a static method in the main activity to display the ProgressDialog. This has the same issue, where the dialog is not being displayed until AFTER the data has already been loaded.Hemia
S
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();
    }
Spurry answered 25/6, 2010 at 21:32 Comment(12)
This gives the exact same exception.Hemia
Then is something wrong with your Context. Change that from this.getApplicationContext() to just this when you pass it. Or if the AsyncTask is private to a Context class, just use the reference to the outer class as MyClass.thisSpurry
Using this instead of this.getApplicationContext() gets rid of the exception, but no dialog is displayed. (pd.dismiss() in onPostExecute also runs without error.)Hemia
@Hemia try looking into the samples in your SDK folder, and above my example, it simply works for an Activity that has no dialogs displayedSpurry
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).Hemia
Please post the whole code of your AsyncTask, and the method where you call execute(). Edit the original question.Spurry
There you go! Thanks again for your help.Hemia
I don't see anything odd there, just that you call fetcher.get() right after you execute the task which runs in asynchronous way, so it will finish long after your code is executed.Spurry
Could it be that the UI thread is busy waiting for the AsyncTask to finish, so it can't display the ProgressDialog? What should I do to prevent this?Hemia
I don't see that happening. Check the sample apps from your SDK samples folder.Spurry
Ok, so I discovered that if I remove the fetch.get() line (the one that is hogging the UI thread), the ProgressDialog works, but now the results obviously aren't being displayed. How can I update an ImageView from my AsyncTask?Hemia
You can do some change in your interface in the onProgressUpdate method, or the onPostExecute both run on the UI thread. Whichever is suitable for you.Spurry
P
1

try this

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

and yes I think the same the problem is with your context.

Presbyterate answered 25/6, 2010 at 22:40 Comment(2)
Using this instead of this.getApplicationContext() gets rid of the exception, but no dialog is displayed. (pd.dismiss() in onPostExecute also runs without error.)Hemia
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).Hemia
F
0

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

Fecteau answered 13/8, 2015 at 16:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.