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!