I am trying to open Time Picker Dialog in Edit Text. though dialog open successfully but when i click ok to set time it throws Exception and getting crash.
Error is as follows:
2020-10-06 10:16:28.972 1821-3134/? E/InputReader: isBtnTouch is true need to clear slots.2020-10-06 10:16:29.029 11973-11973/com.example.visitortracker E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.visitortracker, PID: 11973
java.lang.IllegalArgumentException: Invalid era
at java.util.GregorianCalendar.computeTime(GregorianCalendar.java:2686)
at java.util.Calendar.updateTime(Calendar.java:3402)
at java.util.Calendar.getTimeInMillis(Calendar.java:1761)
at java.util.Calendar.getTime(Calendar.java:1734)
at com.example.visitortracker.VisitorEntryActivity$1.onTimeSet(VisitorEntryActivity.java:102)
at android.app.TimePickerDialog.onClick(TimePickerDialog.java:174)
at android.app.TimePickerDialog$1.onClick(TimePickerDialog.java:156)
My code is as below
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.edt_timeIn:
openTimePicker();
break;
}
}
private void openTimePicker() {
calendar = Calendar.getInstance();
calendar.get(Calendar.HOUR_OF_DAY);
calendar.get(Calendar.MINUTE);
TimePickerDialog dialog = new TimePickerDialog(VisitorEntryActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selected_hour, int selected_minute) {
String am_pm = "am";
if (selected_hour > 12) {
selected_hour = selected_hour - 12;
am_pm = "pm";
}
calendar.set(hour, selected_hour);
calendar.set(minute, selected_minute);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm", Locale.getDefault());
edt_timeIn.setText(simpleDateFormat.format(calendar.getTime()) + " " + am_pm);
}
}, hour, minute, false);
dialog.show();
}
Change
calendar.set(hour, selected_hour);
to calendar.set(Calendar.HOUR_OF_DAY, selected_hour);
and
calendar.set(minute, selected_minute);
to calendar.set(Calendar.MINUTE, selected_minute);