4

I am very confused as to why my dialog is not working correctly:

AlertDialog dialog;
final AlertDialog.Builder builder = new AlertDialog.Builder(this);  
final CharSequence[] confirmCheckbox = {"Delete the SQLite database upon exit."};
final boolean states[] = {false};

builder.setTitle("Delete Database on Exit?");
builder.setMultiChoiceItems(confirmCheckbox, states, new DialogInterface.OnMultiChoiceClickListener(){
    public void onClick(DialogInterface dialogInterface, int item, boolean state) {
    }
}).setMessage("Check the checkbox below to confirm that you wish to delete the SQLite database upon exit.\n" +
        "To cancel this action, hit the back button.")
.setPositiveButton(R.string.confirm_button_text, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        dialog.cancel();
    }
});
dialog = builder.create();
//dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false); <-- This will fail. BUTTON_POSITIVE is NULL
dialog.show();

The first issue I have, is that nothing shows up. I get the title, which has two strange white bars on each side of it, a black screen in the middle, and tue positive button, but I cannot reference the positive button, because i get a Null Pointer when I do.

I have read over many tutorials, and they all look similar to what I have, but I am having no luck. Any ideas?

Soatl
  • 10,224
  • 28
  • 95
  • 153
  • 5
    That's because they're not compatible. Once you set items on the AlertDialog, it can no longer have a message. Couldn't find the reference that taught me this, but ran into this issue over a year ago, myself – Cruceo Jul 18 '14 at 19:40
  • So what would I use instead? The documentation uses AlertDialogs http://developer.android.com/guide/topics/ui/dialogs.html – Soatl Jul 18 '14 at 19:42
  • Have you tried using a custom layout? – Cruceo Jul 18 '14 at 19:44
  • I saw that as an option, but I didn't want to have to reinvent the wheel if I didn't have to. I expected doing something similar to the documentation would work. I may just do that instead. – Soatl Jul 18 '14 at 19:46
  • Also, I was originally wrong about the positive/negative button not being compatible. That's only with single-selection items, but multi-choice items do work with positive/negative buttons – Cruceo Jul 18 '14 at 19:52
  • Why would dialogs don't work with positive/negative buttons? What else would I use it for but changing a selection? I had the same issues and found out my positive button was displayed but its text color was white! So it was not visible and I had to change the style (described here: https://stackoverflow.com/questions/27965662/how-can-i-change-default-dialog-button-text-color-in-android-5). – novas1r1 Oct 30 '19 at 10:46

0 Answers0