Q: Simple app crashes for no apparent reason I am developing a simple android app that has a widget with a button that when pressed will just display an AlertDialog which has some text. There are no errors showing for my code in the IDE. When I click the widgets button a menu pops up saying "One UI Home keeps stopping" and then gives me options to send feedback to android and close the app. I have absolutely no idea what is happening here, I am very new to creating Android apps.
Widgets XML file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#09C"
    android:padding="@dimen/widget_margin">

    <Button
        android:id="@+id/getButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="AlertUser"
        android:text="@string/button_text" />
</RelativeLayout>

MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void AlertUser(View view) {
    new AlertDialog.Builder(getApplicationContext())
            .setTitle("Keycode")
            .setMessage("Test")


            // A null listener allows the button to dismiss the dialog and take no further action.
            .setNegativeButton(android.R.string.no, null)
            .setIcon(android.R.drawable.ic_dialog_alert)
            .show();
}

All other files have been kept as default. Any help appreciated!

A: Try this:
Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
  button = findViewById(R.id.getButton);
  button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view){
        new AlertDialog.Builder(getApplicationContext())
        .setTitle("Keycode")
        .setMessage("Test")


        // A null listener allows the button to dismiss the dialog and take no further action.
        .setNegativeButton(android.R.string.no, null)
        .setIcon(android.R.drawable.ic_dialog_alert)
        .show();
        }
      }
}

And remove the onclick event from the button in the xml
Edit: Sorry for not putting the complete code, Button = button to create a variable that I will use in my app, so when I use button = findViewById I refer to the id in the xml to do an onclick event, all this in the main class, I apologize I don't speak English very well.

A: let's create the dialog in this way:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Activity context);
    alertDialogBuilder
            .setTitle("Keycode")
            .setMessage("Test")
            .setIcon(android.R.drawable.ic_dialog_alert)
            .setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                            int id) {
                            //code here
                        }
                    });
    alertDialogBuilder.setNegativeButton("No",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
    AlertDialog alert = alertDialogBuilder.create();
    alert.show();

