单击时有一个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)
答案 0 :(得分:1)
我遇到了类似的问题,也有类似的解决方案。
maxDate
设置为Long.MAX_VALUE
,打开日历小部件后会使其崩溃maxDate
设置为“从现在开始1000年”而不是Long.MAX_VALUE
即可解决。答案 1 :(得分:0)
问题是由于某些服务器问题,在某些随机情况下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.
}