title: Android catch unhandled exception and show the dialog
body: <p>I want to handle unhandled exception in my app without any third-party libraries.</p>
  
  <p>So i write a code.</p>
  
  <p><strong>Activity :</strong></p>
  
  <pre><code>@Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      Thread.setDefaultUncaughtExceptionHandler(new ReportHelper(this));
      throw new NullPointerException();
  }
  </code></pre>
  
  <p><strong>My crash handler :</strong></p>
  
  <pre><code>import android.app.AlertDialog;
  import android.content.Context;
  import android.content.DialogInterface;
  import android.os.Handler;
  import android.os.Looper;
  import android.os.Message;
  import android.os.MessageQueue;
  import android.widget.Toast;
  
  /**
   * Created by S-Shustikov on 08.06.14.
   */
  public class ReportHelper implements Thread.UncaughtExceptionHandler {
      private final AlertDialog dialog;
      private       Context     context;
  
      public ReportHelper(Context context) {
          this.context = context;
          AlertDialog.Builder builder = new AlertDialog.Builder(context);
          builder.setMessage("Application was stopped...")
                  .setPositiveButton("Report to developer about this problem.", new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int id) {
  
                      }
                  })
                  .setNegativeButton("Exit", new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int id) {
                          // Not worked!
                          dialog.dismiss();
                          System.exit(0);
                          android.os.Process.killProcess(android.os.Process.myPid());
  
                      }
                  });
  
          dialog = builder.create();
      }
  
      @Override
      public void uncaughtException(Thread thread, Throwable ex) {
          showToastInThread("OOPS!");
      }
  
      public void showToastInThread(final String str){
          new Thread() {
              @Override
              public void run() {
                  Looper.prepare();
                  Toast.makeText(context, "OOPS! Application crashed", Toast.LENGTH_SHORT).show();
                  if(!dialog.isShowing())
                      dialog.show();
                  Looper.loop();
  
              }
          }.start();
      }
  }
  </code></pre>
  
  <p>When i start app as you see <strong>i throwed NullPointerException</strong>. <code>Toast</code> in my handling logic was showed, and dialog was showed too. BUT! Dialog clicks was not handling correct. I mean logic in <code>onClick</code> method was not worked. What the problem and how i can fix that?</p>