title: Unable to add window exception while showing a progress bar
body: <p><strong>AIM</strong> I have an activity with a list view and a few buttons and a few extra stuff. What I want is to populate this list view with custom adapter when the activity starts. As it takes time to generate this custom adapter I wanted to add a progress bar, so that user is not forced to stare at the unresponsive-screen. When done, dismiss this progress bar and show the activity as it is supposed to be.</p>
  
  <p><strong>WHAT I DID - "<em>CODE</em>"</strong> </p>
  
  <pre><code>public void onCreate(Bundle savedInstanceState) {
  ...
  // all initialization done here properly
  new ProgressTask(this).execute("");
  }
  
  public class ProgressTask extends AsyncTask&lt;String, Void, Boolean&gt; {
  
      public ProgressTask(ScanInboxActivity activity) {
          dialog = new ProgressDialog(activity.getApplicationContext());
          dialog.setIndeterminate(true);
          dialog.setCancelable(false);
      }
  
          /** progress dialog to show user that the backup is processing. */
      private ProgressDialog dialog;
  
      protected void onPreExecute() {
          this.dialog.setMessage("Scan start");
          this.dialog.show();
      }
  
      @Override
      protected void onPostExecute(final Boolean success) {
          if (dialog.isShowing()) {
              dialog.dismiss();               
          }
          populateList();
      }
  
      protected Boolean doInBackground(final String... args) {
          init(); // tasks to be performed- no error here
          scanSms(); // tasks to be performed- no error here
          return true;
      }
  }
  </code></pre>
  
  <p>I get following Exception:</p>
  
  <pre><code>FATAL EXCEPTION: main
  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.ScanInboxActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
  at android.os.Handler.dispatchMessage(Handler.java:99)
  at android.os.Looper.loop(Looper.java:123)
  at android.app.ActivityThread.main(ActivityThread.java:4627)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:521)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
  at dalvik.system.NativeStart.main(Native Method)
  Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
  at android.view.ViewRoot.setView(ViewRoot.java:509)
  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
  at android.app.Dialog.show(Dialog.java:241)
  at com.example.ScanInboxActivity$ProgressTask.&lt;init&gt;(ScanInboxActivity.java:212)
  at com.example.ScanInboxActivity.onCreate(ScanInboxActivity.java:59)
  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
  ... 11 more
  </code></pre>
  
  <p>Please note that when I write this </p>
  
  <pre><code>public void onCreate(Bundle savedInstanceState) {
  // all initialization done here properly
  init();
  scanSms();
  populateList();
  }
  </code></pre>
  
  <p>Things work fine, though the activity which starts this activity hangs for a moment. I don't want this to happen.</p>
  
  <p>Please suggest what to do. Thanks in advance.</p>
  
  <p><strong>EDIT</strong> scanSms() uses context. I can not eliminate use of context because it is used in a function (in a different class) called in scanSms(), which is also called from other activities around the application</p>