title: "android.view.WindowManager$BadTokenException: Unable to add window" on buider.show()
body: <p>From my main <code>activity</code>, I need to call an inner class and in a method within the class, I need to show <code>AlertDialog</code>. After dismissing it, when the OK button is pressed, forward to Google Play for purchase.</p>
  
  <p>Things work perfectly for most of the times, but for few users it is crashing on <code>builder.show()</code> and I can see <code>"android.view.WindowManager$BadTokenException:</code> Unable to add window" from crash log. Please suggest.</p>
  
  <p>My code is pretty much like this:</p>
  
  <pre><code>public class classname1 extends Activity{
  
    public void onCreate(Bundle savedInstanceState) {
      this.requestWindowFeature(Window.FEATURE_NO_TITLE);
      super.onCreate(savedInstanceState);
      setContentView(R.layout.&lt;view&gt;); 
  
      //call the &lt;className1&gt; class to execute
    }
  
    private class classNamename2 extends AsyncTask&lt;String, Void, String&gt;{
  
      protected String doInBackground(String... params) {}
  
      protected void onPostExecute(String result){
        if(page.contains("error")) 
        {
          AlertDialog.Builder builder = new AlertDialog.Builder(classname1.this);
          builder.setCancelable(true);
          builder.setMessage("");
          builder.setInverseBackgroundForced(true);
          builder.setNeutralButton("Ok",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton){
              dialog.dismiss();
              if(!&lt;condition&gt;)
              {
                try
                {
                  String pl = ""; 
  
                  mHelper.&lt;flow&gt;(&lt;class&gt;.this, SKU, RC_REQUEST, 
                    &lt;listener&gt;, pl);
                }
  
                catch(Exception e)
                {
                  e.printStackTrace();
                }
              }  
            }
          });
  
          builder.show();
        }
      }
    }
  }
  </code></pre>
  
  <p>I have also seen the error in another alert where I am not forwarding to any other <code>activity</code>. It's simple like this:                                                       </p>
  
  <pre><code>AlertDialog.Builder builder = new AlertDialog.Builder(classname1.this);
      builder.setCancelable(true);
  
      //if successful
      builder.setMessage(" ");
      builder.setInverseBackgroundForced(true);
      builder.setNeutralButton("Ok",new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int whichButton){
              // dialog.dismiss();
                     }
      });
      builder.show();
  }
  </code></pre>