Aaron
Aaron

Reputation: 98

Difficulty displaying alert dialog, why does this code force close my app?

I'm very new to Android so I've been working primarily with information from the android developer's page. Everything was going great until I added the code from the alert dialog section. The code they give alone gives me an error when I try to run it on the last line, saying I must initialize dialog, but I feel like I'm getting the NullPointerException no matter what the case is... Heres my code:

protected Dialog onCreateDialog(int id) {
        Dialog dialog = null;
        switch(id) {
        case NAME_MISSING_ID:
            // do the work to define the Dialog
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Proceed without a name?")
                   .setCancelable(false)
                   .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                            MainActivity.this.finish();
                       }
                   })
                   .setNegativeButton("No", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                       }
                   });
            AlertDialog alert = builder.create();
            break;
        case HARD_SELECTION_ID:
            // do the work to define the Dialog
            AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
            builder2.setMessage("This is INSANE! Are you sure?")
                   .setCancelable(false)
                   .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                            MainActivity.this.finish();
                       }
                   })
                   .setNegativeButton("No", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                       }
                   });
            AlertDialog alert2 = builder2.create();
            break;
        default:
            dialog = null;
        }
        return dialog;
    }

If I don't instantiate "dialog" to "null" at the beginning, I cannot run the program. I'm not even trying to do anything crazy yet, any help would be great because I'm having alot of trouble trying to figure out what exactly this code is trying to do.

Thanks guys!

Upvotes: 2

Views: 1249

Answers (2)

st0le
st0le

Reputation: 33545

The problem is you're not returning the dialog...the createDialog is always returning null.

instead of AlertDialog alert = builder.create(); you should

return builder.create();

both cases,obviously.

Upvotes: 1

m_vitaly
m_vitaly

Reputation: 11952

You are returning null in all cases - see the dialog variable, it's never beeing assigned a value.

You probably want to change these lines:
AlertDialog alert = builder.create();
AlertDialog alert2 = builder2.create();

to this:
dialog = builder.create();
dialog = builder2.create();

And better give us the full stack trace next time.

Upvotes: 1

Related Questions