Android - showDialog method, problems with default case in switch statement
-
14-11-2019 - |
Question
I'm programming some custom dialogs and I've run into some trouble with the showDialog
method. For some reason, when I pass the id of the Dialog I want to create to the showDialog
method, it executes both the intended case and the default case.
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
Calendar c = Calendar.getInstance();
mYear = c.get(c.YEAR);
mMonth = c.get(c.MONTH);
mDay = c.get(c.DAY_OF_MONTH);
return new DatePickerDialog(this,
mDateSetListener,
mYear, mMonth, mDay);
default:
return null;
}
}
Solution
updated your code with following code,
DatePickerDialog dpd = null;
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
Calendar c = Calendar.getInstance();
mYear = c.get(c.YEAR);
mMonth = c.get(c.MONTH);
mDay = c.get(c.DAY_OF_MONTH);
dpd = new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
break; // you forgot this line in your code
default:
return null;
}
}
OTHER TIPS
break;
in Your First Case :
If cant Work See this http://www.vogella.de/articles/AndroidDialogs/article.html
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow