Android Fatal Exception: java.lang.NullPointerException при открытии DatePickerDialog - PullRequest
3 голосов
/ 30 апреля 2019

У меня есть EditText, при нажатии которого он должен открыть DatePickerDialog. Но на некоторых устройствах происходит сбой случайно. В Crashlytics он показывает, что он падает на некоторых устройствах с Android версии 8 и некоторых в версии 9. Пожалуйста, дайте мне знать, где я делаю неправильно.

Для DatePicker я устанавливаю минимальную и максимальную дату, которую может выбрать пользователь.

DatePickerDialog dialog = new DatePickerDialog(mContext, date, myCalendar
                    .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                    myCalendar.get(Calendar.DAY_OF_MONTH));

            SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
            inputFormat.setTimeZone(TimeZone.getTimeZone("IST"));
            Date date = new Date();
            try {
                date = inputFormat.parse("2019-01-13T13:00:00Z"); // this is getting from server
            } catch (ParseException e) {
                e.printStackTrace();
            }

            Calendar minCal = Calendar.getInstance();
            minCal.setTime(date);
            dialog.getDatePicker().setMinDate(minCal.getTimeInMillis());
            dialog.getDatePicker().setMaxDate(System.currentTimeMillis());
            // dialog.getDatePicker().setCalendarViewShown(false);
            dialog.show();

Это сообщение о сбое, которое я получаю.

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.SimpleMonthView.getMonthHeight()' on a null object reference
       at android.widget.DayPickerView.onLayout(DayPickerView.java:230)
       at android.view.View.layout(View.java:19962)
       at android.view.ViewGroup.layout(ViewGroup.java:6310)
       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
       at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
       at android.view.View.layout(View.java:19962)
       at android.view.ViewGroup.layout(ViewGroup.java:6310)
       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
       at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
       at android.widget.ScrollView.onLayout(ScrollView.java:1552)
       at android.view.View.layout(View.java:19962)
       at android.view.ViewGroup.layout(ViewGroup.java:6310)
       at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
       at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
       at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
       at android.view.View.layout(View.java:19962)
       at android.view.ViewGroup.layout(ViewGroup.java:6310)
       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
       at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
       at android.view.View.layout(View.java:19962)
       at android.view.ViewGroup.layout(ViewGroup.java:6310)
       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
       at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
       at android.view.View.layout(View.java:19962)
       at android.view.ViewGroup.layout(ViewGroup.java:6310)
       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
       at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
       at android.view.View.layout(View.java:19962)
       at android.view.ViewGroup.layout(ViewGroup.java:6310)
       at com.android.internal.widget.AlertDialogLayout.setChildFrame(AlertDialogLayout.java:356)
       at com.android.internal.widget.AlertDialogLayout.onLayout(AlertDialogLayout.java:349)
       at android.view.View.layout(View.java:19962)
       at android.view.ViewGroup.layout(ViewGroup.java:6310)
       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
       at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
       at android.view.View.layout(View.java:19962)
       at android.view.ViewGroup.layout(ViewGroup.java:6310)
       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
       at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
       at android.view.View.layout(View.java:19962)
       at android.view.ViewGroup.layout(ViewGroup.java:6310)
       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
       at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
       at com.android.internal.policy.DecorView.onLayout(DecorView.java:941)
       at android.view.View.layout(View.java:19962)
       at android.view.ViewGroup.layout(ViewGroup.java:6310)
       at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2671)
       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2376)
       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1488)
       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7182)
       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:935)
       at android.view.Choreographer.doCallbacks(Choreographer.java:747)
       at android.view.Choreographer.doFrame(Choreographer.java:677)
       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:921)
       at android.os.Handler.handleCallback(Handler.java:790)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:192)
       at android.app.ActivityThread.main(ActivityThread.java:6806)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:445)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:875)

1 Ответ

0 голосов
/ 30 апреля 2019

Проблема в том, что minDate была выше, чем maxDate в некоторых случайных случаях из-за некоторых проблем с сервером. Я не обрабатывал сценарий, полагая, что minDate (что я получал) всегда будет меньше, чем System.currentTimeMillis().Поэтому нужно разобраться со сценарием:

            if(System.currentTimeMillis() > minCal.getTimeInMillis()){
                dialog.getDatePicker().setMinDate(minCal.getTimeInMillis());
                dialog.getDatePicker().setMaxDate(System.currentTimeMillis());
                dialog.getDatePicker().setCalendarViewShown(false);
                dialog.show();
            }else{

                //Error message depending on the requirements.

            }

...