Android - showDialog方法,在switch语句中出现默认情况的问题

时间:2012-03-01 04:34:38

标签: android switch-statement android-dialog

我正在编写一些自定义对话框,但我遇到了showDialog方法的问题。出于某种原因,当我将想要创建的Dialog的id传递给showDialog方法时,它会执行预期的大小写和默认大小写。

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;
    }
}

2 个答案:

答案 0 :(得分:1)

第一种情况下的

break;

如果无法工作请参阅此http://www.vogella.de/articles/AndroidDialogs/article.html

答案 1 :(得分:1)

使用以下代码

更新您的代码
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;
    }
}