Search code examples
javaandroidandroid-fragmentsandroid-listfragmentlistactivity

java.lang.ClassCastException: ListActivity cannot be cast to DatePickerDialog


I'm having problems to implement this Material DatePicker due to the Context call that I should probably be doing wrong.

The sample from GitHub works just fine because the Dialog is being created by an Activity.

However, at this particular case, I'm working with a Fragment attached to a ListActivity.

This is how I'm calling it:

Calendar now = Calendar.getInstance();
DatePickerDialog dpd = DatePickerDialog.newInstance(
(DatePickerDialog.OnDateSetListener) getActivity(),
now.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH)
);
dpd.show(getFragmentManager(), "Datepickerdialog");

This line (DatePickerDialog.OnDateSetListener) getActivity() is generating the issue. It is declared as MainActivity.this in the sample, but I can't use ListActivity.this or something similar.

Logcat

Process: kva.ihm, PID: 16218 java.lang.ClassCastException: kva.ihm.ParameterListActivity cannot be cast to com.wdullaer.materialdatetimepicker.date.DatePickerDialog$OnDateSetListener at kva.ihm.ParameterDetailFragment$49.onItemClick(ParameterDetailFragment.java:3839)


Solution

  • Silly mistake.

    As previously mentioned by @Selvin in the comments, the interface call was implemented, therefore, it was wrongly associated by Android Studio auto imports.

    TIP: Do not trust evil auto imports. :)