I have a DatePickerFragment that lets the user select a date from a calendar. The captured date is then set on an EditText line. The below code in onDateSet worked fine with a prior version of the OS now it gives the below error and date is not showing up as expected: mm/dd/yyyy. The error is highlighted on the "txtDate.setText..." line. What am I doing wrong here?
public Dialog onCreateDialog(@NonNull Bundle savedInstanceState) {
super.onCreateDialog(savedInstanceState);
// If the argsbundle from the Activity has data (because the user previously selected a date) then
// set that date in the DatePicker.
if (getArguments() != null) {
c = Calendar.getInstance();
year = getArguments().getInt("year");
month = getArguments().getInt("month");
day = getArguments().getInt("day");
c.set(year, month, day);
} else { // If the DueDate EditText line is empty (no previously selected date by the user then
// set today's date into the DatePicker.
// Calendar class obtains the current date on the device and has fields for
// each of the parts of the date: day, month and year.
c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
}
DatePickerDialog picker = new DatePickerDialog(getActivity(),
this, year, month, day);
// Set monthly calendar rather than default spinner.
picker.getDatePicker().setCalendarViewShown(true);
// Turn off date selector spinner.
picker.getDatePicker().setSpinnersShown(false);
picker.setTitle("Select a Due Date");
return picker;
}
partial DatePickerFragment.java file:
...
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// Set the selected date into the FEditText line.
EditText txtDate = (EditText) getActivity().getWindow().getDecorView().getRootView().findViewById(R.id.FEditText);
// Format the selected date.
txtDate.setText((monthOfYear + 1) + "/" + dayOfMonth + "/" + year + " ");
// Display the date. "Month" uses a zero-based index from 0 to 11, so need to add 1 to show properly.
txtDate.setSelection(txtDate.getText().length());
}