如何修复 MainActivity 无法在片段中转换为 android.app.TimePickerDialog$OnTimeSetListener?

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

我几天前就在 android 做我的学校项目,今天我遇到了一个问题。我做了一个带有片段的菜单,我需要将一个 TimePickerDialog 放在一个片段中,对我来说这是不可能的,每次当我按下按钮启动对话框时,应用程序都会崩溃。

我尝试将工具放在 TimePickerFragment 的 class 中,但它不起作用,我需要将这些东西放在我的主要片段中,而不是对话框中。

这是我的主要片段,叫做 "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);
    }

这是 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()));
    }



}

我希望在 texview 中显示日期,但它在对话框打开之前就崩溃了。

编辑:我忘记放崩溃了:

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.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)

(TimePickerDialog.OnTimeSetListener) this 导致崩溃

要解决此问题,您可以使用以下方法

1 在 TimePickerFragment

中实施 TimePickerDialog.OnTimeSetListener
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. 创建一个对象并使用它

public class TimePickerFragment 扩展 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()));
}

}