How to fix MainActivity cannot be cast to android.app.TimePickerDialog$OnTimeSetListener in a fragment?

◇◆丶佛笑我妖孽 提交于 2020-07-23 06:55:06

问题


i'm doing my school project in android since days ago, and I got a problem today. I did a menu with fragments and I need to put a TimePickerDialog inside a fragment and for me it's impossible, everytime when I press the button lo launch the Dialog the app crashes.

I try to put implements in the class of the TimePickerFragment but it doens't work, I need to bring the things in my main fragment, not in the Dialog.

Here is my main fragment, called "DespertarFragment":

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        //TimePicker picker = getView().findViewById(R.id.alarmTimePicker);
        //picker.setIs24HourView(true);
        View view = inflater.inflate(R.layout.fragment_despertar, container, false);
        mTextView = view.findViewById(R.id.textView1);

        Button buttonTimePicker = view.findViewById(R.id.button);
        buttonTimePicker.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogFragment timePicker = new TimePickerFragment();
                timePicker.show(getFragmentManager(), "Seleccionar");
            }
        });

        Button buttonCancelAlarm = view.findViewById(R.id.button_cancel);
        buttonCancelAlarm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancelAlarm();
            }
        });

        return view;
    }
//This is the function I'm trying to get the time
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, hourOfDay);
        c.set(Calendar.MINUTE, minute);
        c.set(Calendar.SECOND, 0);

        updateTimeText(c);
        startAlarm(c);
    }

And here is the TimePickerFragment class:

public class TimePickerFragment extends DialogFragment {

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);

        return new TimePickerDialog(getActivity(), (TimePickerDialog.OnTimeSetListener) this, hour, minute, DateFormat.is24HourFormat(getActivity()));
    }



}

I expect the date shows in the texview but it crashes before the dialog opens.

Edit: I forgot to put the crash:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.nando.goodnighthommie, PID: 9584
    java.lang.ClassCastException: com.nando.goodnighthommie.TimePickerFragment cannot be cast to android.app.TimePickerDialog$OnTimeSetListener
        at com.nando.goodnighthommie.TimePickerFragment.onCreateDialog(TimePickerFragment.java:23)
        at android.support.v4.app.DialogFragment.onGetLayoutInflater(DialogFragment.java:330)
        at android.support.v4.app.Fragment.performGetLayoutInflater(Fragment.java:1308)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
        at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
        at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
        at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
        at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

回答1:


(TimePickerDialog.OnTimeSetListener) this is causing the crash

To fix this you can use the following approaches

1 implement TimePickerDialog.OnTimeSetListener in TimePickerFragment

public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener{

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);

        return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity()));
    }

    @Override 
    public onTimeSet(TimePicker view, int hourOfDay, int minute) {
       // TODO : implement the functionality
    }
}
  1. create an object and use it

public class TimePickerFragment extends DialogFragment {

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Calendar c = Calendar.getInstance();
    int hour = c.get(Calendar.HOUR_OF_DAY);
    int minute = c.get(Calendar.MINUTE);
    return new TimePickerDialog(getActivity(), new TimePickerDialog.OnTimeSetListener() {
        @Override
        public onTimeSet(TimePicker view, int hourOfDay, int minute) {
            // TODO : implement the functionality
        }
    }, hour, minute, DateFormat.is24HourFormat(getActivity()));
}

}



来源:https://stackoverflow.com/questions/56071179/how-to-fix-mainactivity-cannot-be-cast-to-android-app-timepickerdialogontimeset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!