MaterialDatePicker, app crashes after selecting the end date

Viewed 509

I have a MaterialDatePicker that selects a range of data, for example from 1 January 2021 to 31 January 2021.

MaterialDatePicker.Builder<Pair<Long, Long>> builder = MaterialDatePicker.Builder.dateRangePicker();

CalendarConstraints.Builder constraintsBuilder = new CalendarConstraints.Builder();

builder.setCalendarConstraints(constraintsBuilder.build());

int dialogTheme = resolveOrThrow(requireContext());

builder.setTheme(dialogTheme);

MaterialDatePicker<Pair<Long, Long>> picker = builder.build();

picker.show(requireActivity().getSupportFragmentManager(), picker.toString());

picker.addOnPositiveButtonClickListener(selection -> {

   long startDate = selection.first;
   long endDate = selection.second;

   from = DateFormat.format("yyyy-MM-dd", new Date(startDate)).toString();
   to = DateFormat.format("yyyy-MM-dd", new Date(endDate)).toString();

});


private static int resolveOrThrow(Context context) {

    TypedValue typedValue = new TypedValue();

    if (context.getTheme().resolveAttribute(R.attr.materialCalendarTheme, typedValue, true)) {
        return typedValue.data;
    }

    throw new IllegalArgumentException(context.getResources().getResourceName(R.attr.materialCalendarTheme));
}

When opening the MaterialDatePicker it shows the current date 01 February 2021. When I try to scroll from February to January, the last day of the month is not visible

enter image description here

To select the last day of the month I should scroll down, but when I select the last day of this month the app crashes showing this error:

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getLeft()' on a null object reference at com.google.android.material.datepicker.MaterialCalendarGridView.horizontalMidPoint(MaterialCalendarGridView.java:242) at com.google.android.material.datepicker.MaterialCalendarGridView.onDraw(MaterialCalendarGridView.java:172) at android.view.View.draw(View.java:23915) at android.widget.AbsListView.draw(AbsListView.java:4988) at android.view.View.updateDisplayListIfDirty(View.java:22790) at android.view.View.draw(View.java:23645) at android.view.ViewGroup.drawChild(ViewGroup.java:5336) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:5093) at android.view.View.updateDisplayListIfDirty(View.java:22776) at android.view.View.draw(View.java:23645) at android.view.ViewGroup.drawChild(ViewGroup.java:5336) at androidx.recyclerview.widget.RecyclerView.drawChild(RecyclerView.java:5030) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:5093) at android.view.View.draw(View.java:23918) at androidx.recyclerview.widget.RecyclerView.draw(RecyclerView.java:4429) at android.view.View.updateDisplayListIfDirty(View.java:22790) at android.view.View.draw(View.java:23645) at android.view.ViewGroup.drawChild(ViewGroup.java:5336) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:5093) at android.view.View.updateDisplayListIfDirty(View.java:22776) at android.view.View.draw(View.java:23645) at android.view.ViewGroup.drawChild(ViewGroup.java:5336) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:5093) at android.view.View.updateDisplayListIfDirty(View.java:22776) at android.view.View.draw(View.java:23645) at android.view.ViewGroup.drawChild(ViewGroup.java:5336) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:5093) at android.view.View.updateDisplayListIfDirty(View.java:22776) at android.view.View.draw(View.java:23645) at android.view.ViewGroup.drawChild(ViewGroup.java:5336) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:5093) at android.view.View.updateDisplayListIfDirty(View.java:22776) at android.view.View.draw(View.java:23645) at android.view.ViewGroup.drawChild(ViewGroup.java:5336) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:5093) at android.view.View.updateDisplayListIfDirty(View.java:22776) at android.view.View.draw(View.java:23645) at android.view.ViewGroup.drawChild(ViewGroup.java:5336) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:5093) at android.view.View.updateDisplayListIfDirty(View.java:22776) at android.view.View.draw(View.java:23645) at android.view.ViewGroup.drawChild(ViewGroup.java:5336) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:5093) at android.view.View.updateDisplayListIfDirty(View.java:22776) at android.view.View.draw(View.java:23645) at android.view.ViewGroup.drawChild(ViewGroup.java:5336) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:5093) at android.view.View.updateDisplayListIfDirty(View.java:22776) at android.view.View.draw(View.java:23645) at android.view.ViewGroup.drawChild(ViewGroup.java:5336) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:5093) at android.view.View.draw(View.java:23918) at com.android.internal.policy.DecorView.draw(DecorView.java:1268) at android.view.View.updateDisplayListIfDirty(View.java:22790) at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:567) at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:573) at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:650) at android.view.ViewRootImpl.draw(ViewRootImpl.java:4906) at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:4613) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3754) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2519) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:9775) 2021-02-01 12:06:21.286 10369-10369/com.roko.hearth E/AndroidRuntime: at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1010) at android.view.Choreographer.doCallbacks(Choreographer.java:809) at android.view.Choreographer.doFrame(Choreographer.java:744) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:995) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:246) at android.app.ActivityThread.main(ActivityThread.java:8425) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:596) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)

I tried to look into MaterialCalendarGridView.java:242 as the error indicates and there is this line:

  private static int horizontalMidPoint(@NonNull View view) {
     return view.getLeft() + view.getWidth() / 2;
  }

Does any help please?

1 Answers

It's a late answer but it can be usefull for other people : the crash is fixed on the 1.5.0-alpha01 version of android material components : https://github.com/material-components/material-components-android/releases/tag/1.5.0-alpha01 (check MaterialDatePicker section)

So you can upgrade material components to 1.5.0-alpha01+ or a workaround is to set the fullscreen theme

MaterialDatePicker.Builder.dateRangePicker()
    .setTheme(R.style.ThemeOverlay_MaterialComponents_MaterialCalendar_Fullscreen)
    .setCalendarConstraints(....)
    .build()
Related