开发者

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 onCli开发者_开发知识库ck(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!


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜