Foenix
Foenix

Reputation: 991

Calling DialogFragment from DialogFragment, ClassCastException in getActivity()

I have a DialogFragment1 (it is called by MyFragmentActivity) in my App with date and time field. When user clicks on them the DatePickerFragment and TimePickerFragment appears. They also extend DialogFragments. I want to returm values from them to DailogFragment1 as usual:

public void onDateSet(DatePicker view, int year, int month, int day) {
        EditDateDialogListener activity = (EditDateDialogListener) getActivity();
        activity.onFinishDateDialog(day, month, year);

}

(before I implemented all as usual)

public interface EditDateDialogListener{
void onFinishDateDialog(int day, int month,int year);
}

and in DialogFragment1 that implements EditDateDialogListener:

public void onFinishDateDialog(int day, int month, int year){
this.mDay=day;
this.mMonth=month;
this.mYear=year;
updateDateDisplay();

} But I have got a runtime Error 12-01 20:03:52.018: E/AndroidRuntime(756): java.lang.ClassCastException: MyFragmentActivity cannot be cast to DatePickerFragment$EditDateDialogListener

I understand that this is dialogfragment not activity, but how can I call to it right way? GetWHAT? Thank you for your time.

UPD: my MyFragmentActivity do not implement EditDateDialogListener, because I do not need this Date there, but in DialogFragment1.

public class MyFragmentActivity extends FragmentActivity{

Upvotes: 1

Views: 1601

Answers (2)

Foenix
Foenix

Reputation: 991

I solved this q. like this: in MyFragmentActivity there is a tag:

DialogFragment dlg = MyFragmentActivity.newInstance(p1,p11, p2, p3);
dlg.show(getSupportFragmentManager(), "DataFragmentTag");

and in DatePickerFragment:

public void onDateSet(DatePicker view, int year, int month, int day) {

    EditDateDialogListener handler = (EditDateDialogListener) getActivity().getSupportFragmentManager().findFragmentByTag("DataFragmentTag");
    handler.onFinishDateDialog(day, month, year);
}

Upvotes: 1

Sam
Sam

Reputation: 86958

Addition

my MyFragmentActivity do not implement EditDateDialogListener, because I do not need this Date there, but in DialogFragment1.

I see. Then DailogFragment1 must implement EditDateDialogListener as described below and you need to change onDateSet() to use getParentFragment():

public void onDateSet(DatePicker view, int year, int month, int day) {
    EditDateDialogListener listener = (EditDateDialogListener) getParentFragment();
    listener.onFinishDateDialog(day, month, year);
}

Original Answer

I assume that you are trying to do something like this example in the Developer's Guides.

This error means that MyFragmentActivity does not implement your custom EditDateDialogListener. Simply change this:

public class MyFragementActivity extends FragmentActivity {

To this:

public class MyFragementActivity extends FragmentActivity implements EditDateDialogListener {
//                                             Add this:  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

And override your listener's onFinishDateDialog() callback.


All together change MyFragmentActivity:

public class MyFragementActivity extends FragmentActivity implements EditDateDialogListener {
    @Override
    public void onFinishDateDialog(int day, int month, int year) {
        // Do something
    }
    // The rest of your MyFragmentActivity code here
}

With the change above this line in DatePickerFragment:

EditDateDialogListener activity = (EditDateDialogListener) getActivity();  // It works!

Won't throw any more exceptions.

Upvotes: 2

Related Questions