6

i am using this code to show alert dialog...when i click on the ok button it should show the date picker..

protected Dialog onCreateDialog(int id) {
    switch (id) {
    case TIME_DIALOG_ID:
        return new TimePickerDialog(ListReminderActivity.this,
                mTimeSetListener, mHour, mMinute, false);
    case DATE_DIALOG_ID:
        return new DatePickerDialog(ListReminderActivity.this,
                mDateSetListener, mYear, mMonth, mDay);
    case DESCRIPTION_DIALOG_ID:
        return new AlertDialog.Builder(ListReminderActivity.this).setTitle(
                "Add Reminder").setView(description).setPositiveButton(
                R.string.add_alert_ok,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                            int whichButton) {
                        ListReminderActivity.this
                                .dismissDialog(DESCRIPTION_DIALOG_ID);
                        ListReminderActivity.this
                                .removeDialog(DESCRIPTION_DIALOG_ID);
                        ListReminderActivity.this
                                .showDialog(DATE_DIALOG_ID);
                    }
                }).setNegativeButton(R.string.add_alert_cancel,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                            int whichButton) {
                        ListReminderActivity.this
                                .dismissDialog(DESCRIPTION_DIALOG_ID);
                        ListReminderActivity.this
                                .removeDialog(DESCRIPTION_DIALOG_ID);
                    }
                }).create();
    }
    return null;
}

but the problem is.. i am getting the Exception when i clcik on the ok button

Exception is...

    09-09 10:30:48.941: ERROR/AndroidRuntime(1189): FATAL EXCEPTION: main
09-09 10:30:48.941: ERROR/AndroidRuntime(1189): java.lang.IllegalArgumentException: current should be >= start and <= end
09-09 10:30:48.941: ERROR/AndroidRuntime(1189):     at android.widget.NumberPicker.setCurrent(NumberPicker.java:288)
09-09 10:30:48.941: ERROR/AndroidRuntime(1189):     at android.widget.DatePicker.updateDaySpinner(DatePicker.java:364)
09-09 10:30:48.941: ERROR/AndroidRuntime(1189):     at android.widget.DatePicker.updateSpinners(DatePicker.java:350)
09-09 10:30:48.941: ERROR/AndroidRuntime(1189):     at android.widget.DatePicker.init(DatePicker.java:346)
09-09 10:30:48.941: ERROR/AndroidRuntime(1189):     at android.app.DatePickerDialog.<init>(DatePickerDialog.java:124)
09-09 10:30:48.941: ERROR/AndroidRuntime(1189):     at android.app.DatePickerDialog.<init>(DatePickerDialog.java:83)
09-09 10:30:48.941: ERROR/AndroidRuntime(1189):     at com.app.reminder.ListReminderActivity.onCreateDialog(ListReminderActivity.java:149)
09-09 10:30:48.941: ERROR/AndroidRuntime(1189):     at android.app.Activity.onCreateDialog(Activity.java:2472)
09-09 10:30:48.941: ERROR/AndroidRuntime(1189):     at android.app.Activity.createDialog(Activity.java:881)
09-09 10:30:48.941: ERROR/AndroidRuntime(1189):     at android.app.Activity.showDialog(Activity.java:2547)
09-09 10:30:48.941: ERROR/AndroidRuntime(1189):     at android.app.Activity.showDialog(Activity.java:2514)
09-09 10:30:48.941: ERROR/AndroidRuntime(1189):     at com.app.reminder.ListReminderActivity$3.onClick(ListReminderActivity.java:164)
09-09 10:30:48.941: ERROR/AndroidRuntime(1189):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158)
09-09 10:30:48.941: ERROR/AndroidRuntime(1189):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-09 10:30:48.941: ERROR/AndroidRuntime(1189):     at android.os.Looper.loop(Looper.java:123)
09-09 10:30:48.941: ERROR/AndroidRuntime(1189):     at android.app.ActivityThread.main(ActivityThread.java:4627)
09-09 10:30:48.941: ERROR/AndroidRuntime(1189):     at java.lang.reflect.Method.invokeNative(Native Method)
09-09 10:30:48.941: ERROR/AndroidRuntime(1189):     at java.lang.reflect.Method.invoke(Method.java:521)
09-09 10:30:48.941: ERROR/AndroidRuntime(1189):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-09 10:30:48.941: ERROR/AndroidRuntime(1189):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-09 10:30:48.941: ERROR/AndroidRuntime(1189):     at dalvik.system.NativeStart.main(Native Method)
Kandha
  • 3,659
  • 12
  • 35
  • 50
  • and i am also getting another exception when cancel and then show the dialog – Kandha Sep 09 '10 at 05:02
  • 09-09 10:35:38.610: ERROR/AndroidRuntime(1311): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 09-09 10:35:38.610: ERROR/AndroidRuntime(1311): at android.view.ViewGroup.addViewInner(ViewGroup.java:1970) 09-09 10:35:38.610: ERROR/AndroidRuntime(1311): at android.view.ViewGroup.addView(ViewGroup.java:1865) 09-09 10:35:38.610: ERROR/AndroidRuntime(1311): at android.view.ViewGroup.addView(ViewGroup.java:1845) – Kandha Sep 09 '10 at 05:03
  • whats the value of `mYear`,`mMonth`,`mDay` when the Dialog is being created? did you initialize it to something? – st0le Sep 09 '10 at 05:19
  • yes this is the value for date and time picker...i initialized that values – Kandha Sep 09 '10 at 05:22
  • 1
    That is a huge mess of code. You should write your code so that it's more readable – Falmarri Sep 09 '10 at 05:42
  • Just my two cents: this error does not crash all devices. It seems some swallow the exception and don't display a blank field instead. – rds Jun 20 '11 at 22:05

4 Answers4

9

Do you know that month field is zero based - starts from zero? Make sure that value for month does not exceed 11! I was getting exactly the same message and since I used 12 for a month, that was the reason to get this exception.

dstefanox
  • 2,222
  • 3
  • 19
  • 21
5

As indicated by the error you see in the logs, one of your mYear, mMonth or mDay values is invalid.

Romain Guy
  • 97,993
  • 18
  • 219
  • 200
  • If I remember the DatePicker code correctly, the minimum and maximum possible year depends on the current style? So one theme could crash the app on certain dates while others wouldn't? Also, is there a plan for supporting dates without years? I had solved that back in the days by setting the year to 0, but that no longer works due to the min/max restriction. – EboMike Dec 13 '10 at 04:19
  • I am running into the same problem - my code fails at setting the year - I reduced the year to 2010 manually which works but of course I actually need 2011 (which is the current year right now) :p . I was thinking about your comment - what do you mean by "current style" ? – Ready4Android Aug 27 '11 at 00:57
0

an idea to set the month field value to X is to set it to X % 12 (modulo), so then you are sure that you will not exceed the autorized value. Same for hours and minutes. Cheers. Charles.

Charles Mosndup
  • 600
  • 6
  • 19
-2

you must have changed mYear, mMonth, mDay values. months are 0-11. make sure your values are in the range