wdullaer / MaterialDateTimePicker

Pick a date or time on Android in style
Apache License 2.0
4.67k stars 948 forks source link

showYearPickerFirst(true) cause exception #554

Closed AtaerCaner closed 5 years ago

AtaerCaner commented 6 years ago

Hey, I'm using com.wdullaer:materialdatetimepicker:4.1.0 and when I try to show year picker first with showYearPickerFirst(true) it throws exception as;

java.lang.NullPointerException: Attempt to read from field 'int com.wdullaer.materialdatetimepicker.date.MonthView.mMonth' on a null object reference
        at com.wdullaer.materialdatetimepicker.date.DayPickerView.accessibilityAnnouncePageChanged(DayPickerView.java:355)
        at com.wdullaer.materialdatetimepicker.date.DayPickerGroup.onPageChanged(DayPickerGroup.java:165)
        at com.wdullaer.materialdatetimepicker.date.DayPickerView.lambda$postSetSelection$1(DayPickerView.java:244)
        at com.wdullaer.materialdatetimepicker.date.-$$Lambda$DayPickerView$lwj6z_px0zTXu4FzUTw4TbYiZpk.run(Unknown Source:4)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

My code is;

val datePickerDialog = DatePickerDialog.newInstance {
                _, year, monthOfYear, dayOfMonth ->

            }
            datePickerDialog.accentColor = ContextCompat.getColor(context!!,R.color.cocoa)
            datePickerDialog.showYearPickerFirst(true)
            datePickerDialog.show(getActivity().supportFragmentManager,"DatePickerDialog")

It works without showYearPickerFirst(true).

Thank you.

wdullaer commented 6 years ago

This looks like a bug. I'll look into it.

ricardorover commented 5 years ago

Hi guys, I was having the same problem, did a quick look into it. Looks like getMostVisibleMonth() is returning null in some scenarios, probably only before view layout.

Not sure what is the correct fix, but to keep my app from crashing I did the small fix referenced above, in my fork. Probably some other errors may still appear, because getMostVisiblePosition() (at DayPickerView:259) also uses getMostVisibleMonth() and I think it does not expect null as well.

Hope it helps somehow.

steve-the-edwards commented 5 years ago

Upvote this as I'm having the same issue.

wdullaer commented 5 years ago

The fix @ricardorover applied will work, but it is sort of a symptom patch, rather than a root cause fix.

I've already spent a few hours hunting for the actual problem and made some progress, but it's not done yet. I should have some time in the next week to look at this more.

weiwenhuaming01 commented 5 years ago

Thank's a lot,@ricardorover

steve-the-edwards commented 5 years ago

Yes, thanks @ricardorover , but any update here @wdullaer ? Would be great to remain using your maven release version.

wdullaer commented 5 years ago

I have pushed some code that should resolve this. (I can no longer reproduce it locally)

The core of the issue was that I was trying to measure a View because it was fully drawn. Reshuffling some code ensures that it should be visible now before the code trying to measure it gets called.

It would be great if one or two of the people affected can try the current master branch and confirm that the issue is indeed resolved, before I publish this as a new release.

ricardorover commented 5 years ago

Looks nice @wdullaer. I did a little bit of tests on Android 8.1 and Android 4.1 (devices), and on Android 9 (emulator), the crash did not happen in my test using master branch (commit 0c31fd1).

Thanks for the fix, and thanks for the useful lib.

steve-the-edwards commented 5 years ago

Working for me as well.

To build the release version of the .aar from master I had to remove two lines from layout/mdtp_date_time_picker_dialog.xml (is it used?)

layout_scrollPages from TabLayout

and the scrollBehaviour string from the ViewPager

wdullaer commented 5 years ago

That's a git mistake from a feature branch I tried a while ago. I'll remove that.

Thanks for reporting your feedback!

I'll release this shortly

AtaerCaner commented 5 years ago

It is working. Thank you