Reputation: 1238
Goal is to make a dialog that appears on menu_key pressed, but it keeps force closing. I consulted the official FAQ on Dialogs, but no luck. Everything was done as explained there, but it still fails as soon as the button is pressed. Here is the creation of the dialog:
static final int Choice_ID = 0;
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;
switch(id) {
case Choice_ID:
// do the work to define the pause Dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("TEST DIALOG")
.setCancelable(true);
AlertDialog alert = builder.create();
break;
//default:
//dialog = null;
}
return dialog;
}
And the part about showing looks like this:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
showDialog(Choice_ID);
};
};
Upvotes: 0
Views: 200
Reputation: 86948
This should not cause a force close, but you can see the menu if you try setting the value of dialog
:
dialog = builder.create();
As in:
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;
switch(id) {
case Choice_ID:
// do the work to define the pause Dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("TEST DIALOG")
.setCancelable(true);
dialog = builder.create();
break;
//default:
//dialog = null;
}
return dialog;
}
Upvotes: 1