0

I am trying to implement this DatePickerDialog in my Android App:

public void showDatePickerDialog(View v) {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getFragmentManager(), "datePicker");
}

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);
        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        int mon = month + 1;
        date = day + "/" + mon + "/" + year;
        textview1.setText(date);
    }
}

unfortunatly the App crashes when I try to show the dialog showDatePickerDialog(imageview1); and I get the following Error:

"DatePickerFragment must be a public static class to be properly recreated from instant state"

May someone please help me fix this? I am grateful for every response :)

Vishist Varugeese
  • 1,500
  • 1
  • 17
  • 30
Onno
  • 1
  • This might answer your question https://stackoverflow.com/a/39916305/6845131 – Vishist Varugeese Aug 12 '20 at 11:30
  • For the crash, please attach the stacktrace from logcat. The "public static class" issue is likely not the crash here (it would only crash there e.g. after a configuration change). – laalto Aug 12 '20 at 14:50
  • Thanks to the both of you, I figured it out. This Link helped a lot: https://androidride.com/open-calendar-on-button-click-in-android-example-kotlin-java/ – Onno Aug 12 '20 at 16:28

0 Answers0