title: Creating simple confirmation dialog on button press - Android
body: <p>I am creating a simple stopwatch app and have a button that will stop the chronometer on button press. Very simple see below.</p>
  
  <p>I want to create a confirmation dialog that pops up and asks the user if they are sure they want to stop the chronometer. How can I do this?</p>
  
  <blockquote>
    <p>public void stopClick (View view){</p>
  
  <pre><code>  chronometer.stop();
  
  }
  </code></pre>
  </blockquote>
  
  <p>UPDATE:</p>
  
  <p>Thank you. I now have the below code but receive an error. Also posting logs below.</p>
  
  <blockquote>
    <p>public void stopClick (View view){</p>
  </blockquote>
  
  <pre><code>    AlertDialog.Builder builder = new AlertDialog.Builder(context);
      builder.setTitle(R.string.app_name);
      builder.setMessage("Do you want to stop ?");
      builder.setIcon(R.drawable.ic_launcher);
      builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int id) {
              dialog.dismiss();
              chronometer.stop();    // stop chronometer here
  
          }
      });
      builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int id) {
              dialog.dismiss();
          }
      });
      AlertDialog alert = builder.create();
      alert.show();
  </code></pre>
  
  <p>Log:</p>
  
  <pre><code>03-15 09:21:49.130: I/Process(27748): Sending signal. PID: 27748 SIG: 9
  03-15 09:21:53.550: D/AndroidRuntime(28182): Shutting down VM
  03-15 09:21:53.550: W/dalvikvm(28182): threadid=1: thread exiting with uncaught exception (group=0x42055898)
  
  03-15 09:21:53.560: E/AndroidRuntime(28182): FATAL EXCEPTION: main
  03-15 09:21:53.560: E/AndroidRuntime(28182): java.lang.IllegalStateException: Could not execute method of the activity
  
  03-15 09:21:53.560: E/AndroidRuntime(28182):    at android.view.View$1.onClick(View.java:3838)
  03-15 09:21:53.560: E/AndroidRuntime(28182):    at android.view.View.performClick(View.java:4475)
  03-15 09:21:53.560: E/AndroidRuntime(28182):    at android.view.View$PerformClick.run(View.java:18784)
  03-15 09:21:53.560: E/AndroidRuntime(28182):    at android.os.Handler.handleCallback(Handler.java:730)
  03-15 09:21:53.560: E/AndroidRuntime(28182):    at android.os.Handler.dispatchMessage(Handler.java:92)
  03-15 09:21:53.560: E/AndroidRuntime(28182):    at android.os.Looper.loop(Looper.java:137)
  03-15 09:21:53.560: E/AndroidRuntime(28182):    at android.app.ActivityThread.main(ActivityThread.java:5450)
  03-15 09:21:53.560: E/AndroidRuntime(28182):    at java.lang.reflect.Method.invokeNative(Native Method)
  03-15 09:21:53.560: E/AndroidRuntime(28182):    at java.lang.reflect.Method.invoke(Method.java:525)
  03-15 09:21:53.560: E/AndroidRuntime(28182):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
  03-15 09:21:53.560: E/AndroidRuntime(28182):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
  03-15 09:21:53.560: E/AndroidRuntime(28182):    at dalvik.system.NativeStart.main(Native Method)
  03-15 09:21:53.560: E/AndroidRuntime(28182): Caused by: java.lang.reflect.InvocationTargetException
  03-15 09:21:53.560: E/AndroidRuntime(28182):    at java.lang.reflect.Method.invokeNative(Native Method)
  03-15 09:21:53.560: E/AndroidRuntime(28182):    at java.lang.reflect.Method.invoke(Method.java:525)
  03-15 09:21:53.560: E/AndroidRuntime(28182):    at android.view.View$1.onClick(View.java:3833)
  03-15 09:21:53.560: E/AndroidRuntime(28182):    ... 11 more
  03-15 09:21:53.560: E/AndroidRuntime(28182): Caused by: java.lang.NullPointerException
  03-15 09:21:53.560: E/AndroidRuntime(28182):    at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:143)
  03-15 09:21:53.560: E/AndroidRuntime(28182):    at android.app.AlertDialog$Builder.&lt;init&gt;(AlertDialog.java:360)
  03-15 09:21:53.560: E/AndroidRuntime(28182):    at steel.timer.MainActivity.stopClick(MainActivity.java:56)
  03-15 09:21:53.560: E/AndroidRuntime(28182):    ... 14 more
  </code></pre>