-1

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!

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Tim
  • 125
  • 2
  • 8
  • 1
    pass the activity context in AlertDialog.Builder will resolve your issue – Ali Azaz Alam Aug 08 '19 at 18:55
  • I was told in another thread not to do that, they suggested using `getApplicationContext()` instead as doing what you said gave me Signature errors. – Tim Aug 08 '19 at 18:56
  • I forgot to tell you that .create is also called nevertheless I posted my ans below please review it – Ali Azaz Alam Aug 08 '19 at 19:14

2 Answers2

0

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.

Artudoxo
  • 1
  • 2
  • that is not an issue what you're stating – Ali Azaz Alam Aug 08 '19 at 18:53
  • I'm sorry I don't understand this - That code can't work? Where is `button = findViewById(R.id.getButton);` supposed to go as it cannot just be loose in the class and not having a defining type on it? Also it gives me errors when I put it into the IDE, having an open `{` before the @Override and the start of a method doesn't make sense? – Tim Aug 08 '19 at 18:54
  • I just edited the answer – Artudoxo Aug 08 '19 at 19:08
  • This stops the app from crashing when I press the button, however nothing happens when the button is pressed, and if I run the actual app it crashes for a null error: pastebin.com/rszYXuw2 (line 20 is `button = findViewById(R.id.getButton);`)Also in Android Studio it says "Instant Run detected that you are running on a target device that has a work profile or multiple user accounts. Launching the app under a work profile or another user account on the target device will result in a crash." I only have one account and it is personal? – Tim Aug 09 '19 at 10:39
0

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();
Ali Azaz Alam
  • 1,782
  • 1
  • 16
  • 27