时间选择器不工作并崩溃

Time Picker is not working and getting crashed

我正在尝试在编辑文本中打开时间选择器对话框。虽然对话框成功打开,但是当我单击“确定”设置时间时,它抛出 Exception 并崩溃。

错误如下:

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.onTimeSet(VisitorEntryActivity.java:102)
    at android.app.TimePickerDialog.onClick(TimePickerDialog.java:174)
    at android.app.TimePickerDialog.onClick(TimePickerDialog.java:156)

我的代码如下

@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();
}

改变

calendar.set(hour, selected_hour);calendar.set(Calendar.HOUR_OF_DAY, selected_hour);

calendar.set(minute, selected_minute);calendar.set(Calendar.MINUTE, selected_minute);