This question already has answers here:
Showing TimePickerDialog fragment error

(3个答案)


在11个月前关闭。





我试图在片段中显示一个TimePickerDialog。单击该按钮时,TimePickerDialog不会出现,并且应用程序崩溃。在logcat中有一个类强制转换问题。

AlarmFragment类:

public class AlarmFragment extends Fragment implements TimePickerDialog.OnTimeSetListener {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View alarmView = inflater.inflate(R.layout.fragment_alarm, container, false);

        Button button = (Button) alarmView.findViewById(R.id.end_button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogFragment timePicker = new TimePickerFragment();
                timePicker.show(getActivity().getSupportFragmentManager(), "time picker");
            }
        });

        return alarmView;
    }

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

        TextView textView = (TextView) getActivity().findViewById(R.id.time_textView);
        textView.setText("Hour: " + hourOfDay + " Minute: " + minute);
    }

}


TimePickerFragment类:

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

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


logcat中的错误消息:

2019-07-22 18:07:49.104 4872-4872/mapp.com.sg.thesleepingsheep E/AndroidRuntime: FATAL EXCEPTION: main
    Process: mapp.com.sg.thesleepingsheep, PID: 4872
    java.lang.ClassCastException: mapp.com.sg.thesleepingsheep.MainActivity cannot be cast to android.app.TimePickerDialog$OnTimeSetListener
        at mapp.com.sg.thesleepingsheep.TimePickerFragment.onCreateDialog(TimePickerFragment.java:21)
        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:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6119)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

最佳答案

您的错误是在Fragment(class AlarmFragment ... implements TimePickerDialog.OnTimeSetListener)中实现侦听器,但将Activity作为侦听器传递给Dialog(new TimePickerDialog(getActivity(), ...);。要么在Activity中实现侦听器,要么将Fragment作为侦听器传递。

10-08 17:31