views:

42

answers:

2

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!

A: 

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.

Vitaly Polonetsky
And you can initialize the AlertDialog.Builder builder once at the beginning instead of creating it for each case.
Vitaly Polonetsky
Oh I guess that makes sense, I missed changing that when I added the Alert segment of code to the original dialog code, Im going to try that, but what do you mean by the "full stack trace"? Sorry I've never posted anything before...
Aaron
A: 

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.

st0le
That worked! Thanks alot guys, I sat here and stared at this code for like two hours haha... I really appreciate the help, and so quick too!
Aaron