views:

136

answers:

2

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.

+2  A: 
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();
    }
Pentium10
This gives the exact same exception.
Computerish
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.this`
Pentium10
Using `this` instead of `this.getApplicationContext()` gets rid of the exception, but no dialog is displayed. (`pd.dismiss()` in `onPostExecute` also runs without error.)
Computerish
@Computerish, Pentium10 meant, in your AsyncTask you need to use `YourActivity.this` instead of just `this`. In the `AsyncTask` `this` refers to the `AsyncTask` instance, while `YourActivity.this` refers to the activity that the `AsyncTask` was created in.
Qberticus
@Computerish try looking into the samples in your SDK folder, and above my example, it simply works for an Activity that has no dialogs displayed
Pentium10
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).
Computerish
Please post the whole code of your AsyncTask, and the method where you call execute(). Edit the original question.
Pentium10
There you go! Thanks again for your help.
Computerish
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.
Pentium10
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?
Computerish
I don't see that happening. Check the sample apps from your SDK samples folder.
Pentium10
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`?
Computerish
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.
Pentium10
... MyActivity.this worked for me.
mobibob
A: 

try this

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

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

Jorgesys
Using `this` instead of `this.getApplicationContext()` gets rid of the exception, but no dialog is displayed. (`pd.dismiss()` in `onPostExecute` also runs without error.)
Computerish
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).
Computerish