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.

Computerish
  • 9,590
  • 7
  • 38
  • 49
  • This question has been answered http://stackoverflow.com/questions/1561803/android-progressdialog-show-crashes-with-getapplicationcontext – Ally Jun 26 '10 at 08:29
  • 1
    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. – Computerish Jun 26 '10 at 22:40

3 Answers3

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();
    }
Pentium10
  • 204,586
  • 122
  • 423
  • 502
  • This gives the exact same exception. – Computerish Jun 25 '10 at 21:52
  • 4
    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 Jun 25 '10 at 22:05
  • 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 Jun 26 '10 at 02:39
  • @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 Jun 26 '10 at 08:24
  • 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 Jun 26 '10 at 22:41
  • Please post the whole code of your AsyncTask, and the method where you call execute(). Edit the original question. – Pentium10 Jun 27 '10 at 07:46
  • There you go! Thanks again for your help. – Computerish Jun 27 '10 at 23:38
  • 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 Jun 28 '10 at 07:40
  • 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 Jun 28 '10 at 14:38
  • I don't see that happening. Check the sample apps from your SDK samples folder. – Pentium10 Jun 28 '10 at 14:58
  • 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 Jun 29 '10 at 02:19
  • 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 Jun 30 '10 at 07:56
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.

Jorgesys
  • 124,308
  • 23
  • 334
  • 268
  • 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 Jun 26 '10 at 02:39
  • 1
    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 Jun 26 '10 at 22:38
0

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

Yohanim
  • 3,319
  • 9
  • 52
  • 92