从几天前开始,我就在android上做我的学校项目,今天却遇到了问题。我做了一个带有片段的菜单,我需要在片段中放置一个TimePickerDialog,对我来说,这是不可能的,每当我按下启动该对话框的按钮时,应用就会崩溃。
我尝试将工具放入TimePickerFragment的类中,但它不起作用,我需要将其放入我的主片段中,而不是在Dialog中。
这是我的主要片段,称为“ 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类:
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$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)
答案 0 :(得分:0)
(TimePickerDialog.OnTimeSetListener) this
导致崩溃
要解决此问题,您可以使用以下方法
1种TimePickerDialog.OnTimeSetListener
中的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
}
}
公共类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()));
}
}