Skip to content

[MaterialDatePicker] NPE Crash  #2242

@rbenza

Description

@rbenza

The material date picker crashes sometimes when preselecting a date range. Code to invoke the datepicker:

 private fun launchDatePicker() {
        val now = ZonedDateTime.now().toEpochSecond() * 1000
        val selectionRange = ArrayList<CalendarConstraints.DateValidator>().apply {
            add(DateValidatorPointForward.from(ZonedDateTime.now().minusDays(90).toEpochSecond() * 1000))
            add(DateValidatorPointBackward.before(now))
        }

        val calendarConstraints = CalendarConstraints.Builder()
            .setValidator(CompositeDateValidator.allOf(selectionRange))
            .setStart(ZonedDateTime.now().minusDays(90).toEpochSecond() * 1000)
            .setOpenAt(now)
            .setEnd(now)
            .build()

        val datePicker = MaterialDatePicker.Builder.dateRangePicker()
            .setTitleText(getString(R.string.date_range_picker_title))
            .setCalendarConstraints(calendarConstraints)
            .setSelection(Pair(ZonedDateTime.now().minusDays(7L).toEpochSecond().times(1000), now))
            .build()

        datePicker.addOnPositiveButtonClickListener { pair: Pair<Long, Long> ->
            pair.let {
                viewModel.getItemsWithinDateRange(it.first ?: now, it.second ?: now)
            }
        }
        datePicker.show(requireActivity().supportFragmentManager, "date picker")

Crash log:

E/AndroidRuntime: FATAL EXCEPTION: main
     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:156)
        at android.view.View.draw(View.java:23901)
        at android.widget.AbsListView.draw(AbsListView.java:4988)
        at android.view.View.updateDisplayListIfDirty(View.java:22776)
        at android.view.View.draw(View.java:23631)
        at android.view.ViewGroup.drawChild(ViewGroup.java:5336)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:5093)
        at android.view.View.updateDisplayListIfDirty(View.java:22762)
        at android.view.View.draw(View.java:23631)
        at android.view.ViewGroup.drawChild(ViewGroup.java:5336)
        at androidx.recyclerview.widget.RecyclerView.drawChild(RecyclerView.java:5204)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:5093)
        at android.view.View.draw(View.java:23904)
        at androidx.recyclerview.widget.RecyclerView.draw(RecyclerView.java:4603)
        at android.view.View.updateDisplayListIfDirty(View.java:22776)
        at android.view.View.draw(View.java:23631)
        at android.view.ViewGroup.drawChild(ViewGroup.java:5336)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:5093)

Expected behavior: Not crash. The date picker does show briefly, but then the whole app crashes. Something to do with the preselected/visible date range. Our code preselects the last 7 days up until today.

If I manually set the device date to June 6th (instead of the current day, June 7th), then date picker does not crash and works fine. The same goes for June 8th. Only when the device date is June 7th (today) then date picker crashes. A quick fix would be nice, since this is causing a lot of crashes for users.

Tested on Samsung S20 and Emulator, both Android 11. This happens with both version 1.3.0 and 1.4.0-rc01. Have not tried any other versions/devices. A screenshot of how the date picker looks right before the crash:

2021-06-07_10h31_36

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions