android 致命异常:java.lang.NullPointerException 打开 DatePickerDialog 时
android Fatal Exception: java.lang.NullPointerException when opening the DatePickerDialog
我有一个 EditText,点击它应该会打开 DatePickerDialog。但在某些设备中,它会随机崩溃。在 Crashlytics 中,它显示它在某些使用 android 版本 8 的设备和一些使用版本 9 的设备中崩溃。请让我知道我哪里做错了。
对于 DatePicker,我正在设置用户可以设置的最小和最大日期 select。
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)
问题是在某些随机情况下 minDate 高于 maxDate,因为某些服务器 issues.I 没有处理认为 minDate(我得到的)总是小于 [=11] 的场景=].所以需要处理场景:
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.
}
我有类似的问题,有类似的解决方案。
- 我的
maxDate
设置为 Long.MAX_VALUE
,这会导致日历小部件在打开时崩溃
- 将
maxDate
设置为“从现在起 1000 年”而不是 Long.MAX_VALUE
可以修复它。
我有一个 EditText,点击它应该会打开 DatePickerDialog。但在某些设备中,它会随机崩溃。在 Crashlytics 中,它显示它在某些使用 android 版本 8 的设备和一些使用版本 9 的设备中崩溃。请让我知道我哪里做错了。
对于 DatePicker,我正在设置用户可以设置的最小和最大日期 select。
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)
问题是在某些随机情况下 minDate 高于 maxDate,因为某些服务器 issues.I 没有处理认为 minDate(我得到的)总是小于 [=11] 的场景=].所以需要处理场景:
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.
}
我有类似的问题,有类似的解决方案。
- 我的
maxDate
设置为Long.MAX_VALUE
,这会导致日历小部件在打开时崩溃 - 将
maxDate
设置为“从现在起 1000 年”而不是Long.MAX_VALUE
可以修复它。