gpt4 book ai didi

java - 单击 Fragment 中的按钮以显示 TimePickerDialog 时应用程序崩溃

转载 作者:行者123 更新时间:2023-12-02 02:46:01 25 4
gpt4 key购买 nike

我正在尝试在 fragment 中显示 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 ...实现TimePickerDialog.OnTimeSetListener),但将您的 Activity 作为监听器传递给对话框(new TimePickerDialog(getActivity() , ...);。要么在您的 Activity 中实现监听器,要么将您的 Fragment 作为监听器传递。

关于java - 单击 Fragment 中的按钮以显示 TimePickerDialog 时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57143748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com