title: Fatal Exception: java.lang.IllegalArgumentException occurs from ProgressDialog
body: <p>I've 10 apps which uses AsyncTasks. As you know i show progressdialog in asynctasks to show progress of the task. But there is a problem that i couldn't solve so far about progressdialog.</p>
  
  <p>Here is one of my AsyncTask class (which is not in a other class);</p>
  
  <pre><code>public class GetBalanceAsyncTask extends AsyncTask&lt;Void, Void, String&gt; {
  
  Context context;
  ProgressDialog pd;
  
  public GetBakiyeAsyncTask(Context context) {
      this.context = context;
  }
  
  @Override
  protected String doInBackground(Void... arg0) {
      String userAgent = HttpHelper.getRandomUserAgent(context);
      return HttpHelper.post(PreferenceHelper.getBalanceQueryAPI(context), userAgent);
  
  }
  
  @Override
  protected void onPreExecute() {
     super.onPreExecute();
     pd = ProgressDialog.show(context, "","Please wait...",true);
  }
  
  @Override
  protected void onPostExecute(String result) {
      try {
          if (pd != null &amp; pd.isShowing()) {
              pd.dismiss();
              pd = null;
          }
      } catch (IllegalArgumentException e) {
          pd = null;
          e.printStackTrace();
      }
      if (result != null &amp; result.length() &gt; 0) {
          Utils.doTask(result);
      } else {
          Utils.ShowToast(context,
                  "An error has occurred, please try again.",
                  STYLE_CONFIRM, LENGTH_LONG);
      }
  
      super.onPostExecute(result);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>and the execption is</p>
  
  <pre><code>java.lang.IllegalArgumentException: View not attached to window manager
     at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:381)
     at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:226)
     at android.view.Window$LocalWindowManager.removeView(Window.java:432)
     at android.app.Dialog.dismissDialog(Dialog.java:278)
     at android.app.Dialog.access$000(Dialog.java:71)
     at android.app.Dialog$1.run(Dialog.java:111)
     at android.app.Dialog.dismiss(Dialog.java:268)
     at fragments.RuyalarFragment$getRuyalarAsyncTask.onPostExecute(GetBalanceAsyncTask.java:27)
     at fragments.RuyalarFragment$getRuyalarAsyncTask.onPostExecute(GetBalanceAsyncTask.java:1)
     at android.os.AsyncTask.finish(AsyncTask.java:417)
     at android.os.AsyncTask.access$300(AsyncTask.java:127)
     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
     at android.os.Handler.dispatchMessage(Handler.java:99)
     at android.os.Looper.loop(Looper.java:130)
     at android.app.ActivityThread.main(ActivityThread.java:3691)
     at java.lang.reflect.Method.invokeNative(Method.java)
     at java.lang.reflect.Method.invoke(Method.java:507)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
     at dalvik.system.NativeStart.main(NativeStart.java)
  </code></pre>
  
  <p>I've searched the solution almost everywhere but there is no working results. Most of answers contain use isFinishing, onDestroy or something like this but no body is sure which is working. </p>
  
  <p>Thanks for advice.</p>