因此,我是一个开始者,尝试从片段中打开按钮时无法打开时间选择器对话框时遇到麻烦。片段Work
具有按钮(timeButton
),当我按下它时,应用程序崩溃。我将提供错误消息和片段。
这是Work
片段:
public class Work extends Fragment implements TimePickerDialog.OnTimeSetListener {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_work, container, false);
getActivity().setTitle("Work");
Button timeButton = (Button) view.findViewById(R.id.timeButton);
timeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DialogFragment timePicker = new TimePicker_Fragment();
timePicker.show(getFragmentManager(), "Time Picker");
}
});
return view;
}
@Override
public void onTimeSet(TimePicker timePicker, int i, int i1) {
}
}
这是我的time dialog
片段:
public class TimePicker_Fragment extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable 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) getActivity(), hour, minute, DateFormat.is24HourFormat(getActivity()));
}
}
这些是我收到的errors
:
2019-07-02 17:29:15.419 30283-30283/com.example.worktime E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.worktime, PID: 30283
java.lang.ClassCastException: com.example.worktime.MainActivity cannot be cast to android.app.TimePickerDialog$OnTimeSetListener
at com.example.worktime.TimePicker_Fragment.onCreateDialog(TimePicker_Fragment.java:21)
at androidx.fragment.app.DialogFragment.onGetLayoutInflater(DialogFragment.java:330)
at androidx.fragment.app.Fragment.performGetLayoutInflater(Fragment.java:1308)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:802)
at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
at androidx.fragment.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7319)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934)
2019-07-02 17:29:15.541 30283-30283/com.example.worktime I/Process: Sending signal. PID: 30283 SIG: 9
我该如何解决此问题?
答案 0 :(得分:0)
实现 TimePickerDialog.OnTimeSetListener
在您的 TimePicker_Fragment 类中。
答案 1 :(得分:0)
在例外情况下阅读此行。
java.lang.ClassCastException: com.example.worktime.MainActivity cannot be cast to android.app.TimePickerDialog$OnTimeSetListener
这是ClassCastException,因为
return new TimePickerDialog(getActivity(), (TimePickerDialog.OnTimeSetListener) getActivity(), hour, minute, DateFormat.is24HourFormat(getActivity()));
您在此处使用的上下文属于MainActivity
在MainActivity中实现TimePickerDialog.OnTimeSetListener将解决您的问题。