0

I join timepicker and datepicker. Except that I must add limit to date. Unfortunataly I got an error, with which I can't deal with. Maybe someone can help... Error occured on datePicker.updateDate(oldy, oldm, oldd);

Code from my dialog DateTimePicker:

        datePicker = (DatePicker) findViewById(R.id.datePickerDate);
    timePicker = (TimePicker) findViewById(R.id.timePickerTime);

    datePicker.init(DAFAULT_START_YEAR, DEFAULT_START_MONTH, DEFAULT_START_DAY,
        new OnDateChangedListener() {
        @Override
        public void onDateChanged(final DatePicker view, final int year, final int monthOfYear,
                        final int dayOfMonth) {
                    Date currChoice = new Date(year, monthOfYear, dayOfMonth);
                    if (currChoice.before(currentDateTime) || currChoice.equals(currentDateTime)) {
                        //do some things
                    } else {
                        datePicker.updateDate(oldy, oldm, oldd);
                    }
                }
            });
    datePicker.updateDate(y, m, d);

Error:

Thread [<1> main] (Suspended (exception StackOverflowError))    
DataSetObservable.notifyChanged() line: 37  
CalendarView$WeeksAdapter(BaseAdapter).notifyDataSetChanged() line: 50  
CalendarView$WeeksAdapter.setSelectedDay(Calendar) line: 1005   
CalendarView.goTo(Calendar, boolean, boolean, boolean) line: 750    
CalendarView.setDate(long, boolean, boolean) line: 623  
DatePicker.updateCalendarView() line: 636   
DatePicker.updateDate(int, int, int) line: 496  
DateTimeDialog.init() line: 151 
DateTimeDialog.<init>(Context, String) line: 88 
MyActivity$3.onClick(View) line: 395    
EditText(View).performClick() line: 3117    
View$PerformClick.run() line: 11941 
ViewRoot(Handler).handleCallback(Message) line: 587 
ViewRoot(Handler).dispatchMessage(Message) line: 92 
Looper.loop() line: 132 
ActivityThread.main(String[]) line: 4123    
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
Method.invoke(Object, Object...) line: 491  
ZygoteInit$MethodAndArgsCaller.run() line: 841  
ZygoteInit.main(String[]) line: 599 
NativeStart.main(String[]) line: not available [native method]  

Logcat:

10-31 14:53:49.390: E/AndroidRuntime(5348): FATAL EXCEPTION: main
10-31 14:53:49.390: E/AndroidRuntime(5348): java.lang.StackOverflowError
10-31 14:53:49.390: E/AndroidRuntime(5348):     at android.view.View.invalidate(View.java:7178)
10-31 14:53:49.390: E/AndroidRuntime(5348):     at android.view.View.invalidate(View.java:7159)
10-31 14:53:49.390: E/AndroidRuntime(5348):     at android.widget.TextView.invalidateCursor(TextView.java:3826)
10-31 14:53:49.390: E/AndroidRuntime(5348):     at android.widget.TextView.spanChange(TextView.java:6933)
10-31 14:53:49.390: E/AndroidRuntime(5348):     at android.widget.TextView$ChangeWatcher.onSpanAdded(TextView.java:7059)
10-31 14:53:49.390: E/AndroidRuntime(5348):     at android.text.SpannableStringBuilder.sendSpanAdded(SpannableStringBuilder.java:918)
10-31 14:53:49.390: E/AndroidRuntime(5348):     at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:615)
10-31 14:53:49.390: E/AndroidRuntime(5348):     at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:518)
10-31 14:53:49.390: E/AndroidRuntime(5348):     at android.text.Selection.setSelection(Selection.java:74)
10-31 14:53:49.390: E/AndroidRuntime(5348):     at android.text.Selection.setSelection(Selection.java:85)
10-31 14:53:49.390: E/AndroidRuntime(5348):     at android.text.method.ArrowKeyMovementMethod.initialize(ArrowKeyMovementMethod.java:288)
10-31 14:53:49.390: E/AndroidRuntime(5348):     at android.widget.TextView.setText(TextView.java:2814)
10-31 14:53:49.390: E/AndroidRuntime(5348):     at android.widget.TextView.setText(TextView.java:2691)
10-31 14:53:49.390: E/AndroidRuntime(5348):     at android.widget.EditText.setText(EditText.java:78)
10-31 14:53:49.390: E/AndroidRuntime(5348):     at android.widget.TextView.setText(TextView.java:2666)
10-31 14:53:49.390: E/AndroidRuntime(5348):     at android.widget.NumberPicker.updateInputTextView(NumberPicker.java:1427)
10-31 14:53:49.390: E/AndroidRuntime(5348):     at android.widget.NumberPicker.setDisplayedValues(NumberPicker.java:1041)
10-31 14:53:49.390: E/AndroidRuntime(5348):     at android.widget.DatePicker.updateSpinners(DatePicker.java:593)
10-31 14:53:49.390: E/AndroidRuntime(5348):     at android.widget.DatePicker.updateDate(DatePicker.java:495)
10-31 14:53:49.390: E/AndroidRuntime(5348):     at myapp.dialog.DateTimeDialog$3.onDateChanged(DateTimeDialog.java:146)
10-31 14:53:49.390: E/AndroidRuntime(5348):     at android.widget.DatePicker.notifyDateChanged(DatePicker.java:666)
10-31 14:53:49.390: E/AndroidRuntime(5348):     at android.widget.DatePicker.updateDate(DatePicker.java:497)
10-31 14:53:49.390: E/AndroidRuntime(5348):     at myapp.dialog.DateTimeDialog$3.onDateChanged(DateTimeDialog.java:146)
10-31 14:53:49.390: E/AndroidRuntime(5348):     at android.widget.DatePicker.notifyDateChanged(DatePicker.java:666)
10-31 14:53:49.390: E/AndroidRuntime(5348):     at android.widget.DatePicker.updateDate(DatePicker.java:497)
10-31 14:53:49.390: E/AndroidRuntime(5348):     at myapp.dialog.DateTimeDialog$3.onDateChanged(DateTimeDialog.java:146)
10-31 14:53:49.390: E/AndroidRuntime(5348):     at android.widget.DatePicker.notifyDateChanged(DatePicker.java:666)
etc
1
  • You are calling datePicker.updateDate(..) from inside your listener causing your listener to be triggered again, i.e. a loop that will exhaust your stack.
    – Jens
    Oct 31, 2011 at 14:15

2 Answers 2

2

Your call to updateDate() from inside onDateChanged() causes the same event to fire, again and again and again. This happens until you run out of stack space.

Use a boolean variable or something in your onDateChanged() implementation to avoid triggering the event recursively.

2
  • @Reuben We all started at some point or another and made mistakes. No need to get mean about it. The point of Stackover flow is to help people learn, not chastise them for ignorance. Oct 31, 2011 at 14:21
  • I am well aware of the purpose of stackoverflow.com thank you @Kurtis and disagree that I was 'mean' or even chastising. Oct 31, 2011 at 15:36
2

You're calling updateDate from onDateChanged, causing infinite recursion. It looks like you're trying to filter out past dates. Maybe try DatePicker.setMinDate instead.

Update

To be a little more accurate, your methodology isn't too flawed. Programmers often have to set values from listeners themselves. The problem you're having is that you aren't doing anything to kill the recursion. Look at your code below:

Date currChoice = new Date(year, monthOfYear, dayOfMonth);
if (currChoice.before(currentDateTime) || currChoice.equals(currentDateTime)) {
    //do some things
} else {
    datePicker.updateDate(oldy, oldm, oldd);
}

If the current chosen date is less than or equal to the current date, you're setting the value back to a previous value but you are not checking if this previous value is also less than or equal to the current date. That means if the old date is less than the current one you are going to recurse forever. A second check would correct this:

Date currChoice = new Date(year, monthOfYear, dayOfMonth);
Date oldChoice = new Date(oldy, oldm, oldd);
if (currChoice.before(currentDateTime) || currChoice.equals(currentDateTime)) {
    //do some things
} else if (oldChoice.before(currentDateTime) || oldChoice.equals(currentDateTime)) {
    // Maybe do nothing, maybe set the date to tomorrow.  Your call.
} else {
    datePicker.updateDate(oldy, oldm, oldd);
}

Of course, I still think setMinDate is your best choice, but that is what's actually going on.

1
  • Thank you very much for help and forbearance:) I must use API 10, method setMinDate is available from API 11... Nov 1, 2011 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.