1

I get a NullPointerException when I set minDate and maxDate in onCreate of a CalendarActivity. I am desperate looking for the solution.

When I comment minDate and maxDate lines there is no problem.

I call Calendar with a date range (taked with an AsyncTask via http POST), when I change this range and increase it, sometimes there is no problem too.

My Logcat:

06-17 12:32:39.636  21968-21968/com.example.lissar W/EGL_genymotion﹕ eglSurfaceAttrib not implemented
06-17 12:32:39.660  21968-21968/com.example.lissar W/EGL_genymotion﹕ eglSurfaceAttrib not implemented
06-17 12:32:39.664  21968-21968/com.example.lissar D/AndroidRuntime﹕ Shutting down VM
06-17 12:32:39.664  21968-21968/com.example.lissar W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa4c00648)
06-17 12:32:39.676  21968-21968/com.example.lissar E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
        at android.widget.CalendarView.onScroll(CalendarView.java:1214)
        at android.widget.CalendarView.access$900(CalendarView.java:76)
        at android.widget.CalendarView$2.onScroll(CalendarView.java:1075)
        at android.widget.AbsListView.invokeOnItemScrollListener(AbsListView.java:1340)
        at android.widget.ListView.layoutChildren(ListView.java:1762)
        at android.widget.AbsListView.onLayout(AbsListView.java:2012)
        at android.view.View.layout(View.java:14289)
        at android.view.ViewGroup.layout(ViewGroup.java:4562)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
        at android.view.View.layout(View.java:14289)
        at android.view.ViewGroup.layout(ViewGroup.java:4562)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
        at android.view.View.layout(View.java:14289)
        at android.view.ViewGroup.layout(ViewGroup.java:4562)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
        at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1660)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1436)
        at android.view.View.layout(View.java:14289)
        at android.view.ViewGroup.layout(ViewGroup.java:4562)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
        at android.view.View.layout(View.java:14289)
        at android.view.ViewGroup.layout(ViewGroup.java:4562)
        at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:349)
        at android.view.View.layout(View.java:14289)
        at android.view.ViewGroup.layout(ViewGroup.java:4562)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
        at android.view.View.layout(View.java:14289)
        at android.view.ViewGroup.layout(ViewGroup.java:4562)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
        at android.view.Choreographer.doCallbacks(Choreographer.java:562)
        at android.view.Choreographer.doFrame(Choreographer.java:532)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5103)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)

I paste the code where the minDate and maxDate methods are in. At the end of onCreate:

calendarView = (CalendarView) findViewById(R.id.calendarView1);

        calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
                String yearFormat = "", monthFormat = "", dayFormat = "";
                // El mes empieza en 0, así que aumentamos en uno para que cada mes coincida
                // con su número correspondiente: enero -> 1, febrero -> 2, etc...
                month += 1;
                if (month < 10) {
                    monthFormat = String.format("%02d", month);
                } else {
                    monthFormat = Integer.toString(month);
                }
                if (dayOfMonth < 10) {
                    dayFormat = String.format("%02d", dayOfMonth);
                } else {
                    dayFormat = Integer.toString(dayOfMonth);
                }
                yearFormat = Integer.toString(year);
                // Calculamos el día de la semana para mostrarlo en el Dialog
                Calendar selected = Calendar.getInstance();
                selected.setTimeInMillis(view.getDate());
                int dayOfWeek = selected.get(Calendar.DAY_OF_WEEK);
                HoursDialog hoursDialog = new HoursDialog(CalendarActivity.this,
                        yearFormat, monthFormat, dayFormat, dayOfWeek);
                hoursDialog.show();
            }
        });
    GregorianCalendar cal = new GregorianCalendar();
    GregorianCalendar cal2 = new GregorianCalendar(); // Hoy

    int daysBefore = daysEnabled * -1;

    cal.add(GregorianCalendar.DATE, daysBefore);

    calendarView.setMinDate(cal.getTime().getTime());
    calendarView.setMaxDate(cal2.getTime().getTime());

How can I solve this error?

moffeltje
  • 4,521
  • 4
  • 33
  • 57
Roberto Alonso
  • 1,181
  • 1
  • 7
  • 9
  • Why do you call `.getTime()` twice? (`cal.getTime().getTime()`) – moffeltje Jun 17 '15 at 11:13
  • Cause from the first getTime() I get a Date, then I have to do another getTime() to get Time in Millis. Another way to get that is doing getTimeInMillis(), equals to getTime().getTime(). Thank you for your edit, improving my poor english ;) – Roberto Alonso Jun 17 '15 at 12:00
  • Possible duplicate of [Setting MinDate on DatePicker moves CalendarView to 1964](https://stackoverflow.com/questions/16664562/setting-mindate-on-datepicker-moves-calendarview-to-1964) – Roberto Alonso Aug 23 '19 at 11:18

1 Answers1

0

I answer myself.

Calendar Activity has some bugs. I finally used Caldroid: https://github.com/roomorama/Caldroid

Roberto Alonso
  • 1,181
  • 1
  • 7
  • 9