Java – Clicking the button in the AlertDialog in onOptionsItemSelected crashes the emulator

Clicking the button in the AlertDialog in onOptionsItemSelected crashes the emulator… here is a solution to the problem.

Clicking the button in the AlertDialog in onOptionsItemSelected crashes the emulator

My main question is: can I trigger the AlertDialog from inside onOptionsItemSelected() anyway without crashing my emulator when I press a button on the dialog?

For this, I searched all over the internet, but I found that everyone was saying the same thing, but even though I copied and pasted their code, I got the same error. So I will try to explain my situation.

Here’s a very simple example of an AlertDialog:

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this)
                    .setTitle("Your Title")
                    .setMessage("Click yes to exit!")
                    .setCancelable(false)
                    .setNeutralButton("Ok",new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int id) {
                            dialog.cancel();
                        }
                    });

 create alert dialog
            AlertDialog alertDialog = alertDialogBuilder.create();
             show it
            alertDialog.show();

It’s ok now. As long as I run it outside of the onOptionsItemSelected() function. For example, if I add this code to the onClickListener of a regular button. Then, when I click the button, a warning dialog box appears and everything works fine. However, when I include this code in my onOptionsItemSelected(). Then a warning dialog comes up, but pressing a button on the dialog crashes my entire emulator. This is my onOptionsItemSelected():

@Override
public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()){

case R.id.action_trash:

Log.i("trash", "button clicked");

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this)
                    .setTitle("Your Title")
                    .setMessage("Click yes to exit!")
                    .setCancelable(false)
                    .setNeutralButton("Ok",new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int id) {
                            dialog.cancel();
                        }
                    });

 create alert dialog
            AlertDialog alertDialog = alertDialogBuilder.create();
             show it
            alertDialog.show();

return true;
        case R.id.action_help:

Log.i("help", "button clicked");

return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

Now when I click on the item linked to the action_trash id in the toolbar. I get a warning dialog, but when I click the OK button, my entire Android emulator crashes. The only warning I can still see is:

Hax is enabled
Hax ram_size 0x40000000
HAX is working and emulator runs in fast virt mode.
emulator: Listening for console connections on port: 5554
emulator: Serial number of this emulator (for ADB): emulator-5554
EmuGL:WARNING: bad generic pointer 0x7fc16d378600

I’m pretty sure this is an irrelevant message. Sometimes I see someone asking for LogCat, but since the whole emulator crashes, I can’t find anything.

And a side question: why doesn’t this work at all? Is it because the onClickListener() created in setNeutralButton() was somehow broken? I’m new to Android, so any suggestions would be appreciated if this is a big nooby bug that can be avoided in the future.

P.S. I also tried to put “. Builder(this)” to be replaced with”. Builder (MainActivity.this)” and all the variants I’ve come across so far, but none of them solve the problem.

Thanks in advance 🙂

Solution

I’ve come across the same thing. There are two things that work for me.

  1. Change the AVD settings to Software – GLES 2.0 instead of automatic for hardware or the Emulate Performance option.
  2. Turn on Show Layout Boundaries in AVD’s developer options.

One of them should fix the problem, you don’t need to do both.

Related Problems and Solutions