title: How to display AlertDialog in a Fragment?
body: <p>I want to display an alert dialog in my app. I am using fragments. I tried the below code to do this:</p>
  
  <pre><code> AlertDialog ad = new AlertDialog.Builder(context)
              .create();
      ad.setCancelable(false);
      ad.setTitle(title);
      ad.setMessage(message);
      ad.setButton(context.getString(R.string.ok_text), new DialogInterface.OnClickListener() {
  
          public void onClick(DialogInterface dialog, int which) {
              dialog.dismiss();
          }
      });
  ad.show();
  </code></pre>
  
  <p>but it was crashing and the error in logcat was:</p>
  
  <blockquote>
    <p>04-18 15:23:01.770: E/AndroidRuntime(9424): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application</p>
  </blockquote>
  
  <p>From internet I came to know that the crash is due to context issue. I had given context as</p>
  
  <pre><code>context = this.getActivity().getApplicationContext();
  </code></pre>
  
  <p>I don't know what is the problem with this. Can anybody help me?    </p>