gpt4 book ai didi

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

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

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

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

这是我的主要片段,称为“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)

最佳答案

(TimePickerDialog.OnTimeSetListener) 这 导致崩溃

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

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
}
}
  • 创建一个对象并使用它
  • 公共(public)类 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()));
    }

    }

    关于java - 如何修复MainActivity无法在片段中转换为android.app.TimePickerDialog$OnTimeSetListener?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56071179/

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