I have an EditText when clicked it should open DatePickerDialog. But in some devices it is crashing randomly. In Crashlytics it is showing it is crashing in some devices with android version 8 and some in version 9. Please let me know where i am doing wrong.
For DatePicker i am setting minimum and maximum date a user can 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();
This is the crash message i am getting.
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)
Issue was the minDate was higher than the maxDate in some random cases because of some server issues.I was not handling the scenario thinking the minDate(What i was getting) will always be lesser than the System.currentTimeMillis()
. So need to handle the scenario :
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.
}