当来自另一个片段的按钮试图打开它时,时间选择器对话框使应用程序崩溃
Time picker dialog crashes app when button from an onther fragment tries to open it
所以我是初学者,在尝试从片段按下按钮时打开时间选择器对话框时遇到了问题。片段 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.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
我该如何解决这个问题?
在您的TimePicker_Fragmentclass中实施TimePickerDialog.OnTimeSetListener
。
您可以阅读更多关于自定义 TimePicker 的信息 link
在你的异常中阅读这一行。
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 将解决您的问题。
所以我是初学者,在尝试从片段按下按钮时打开时间选择器对话框时遇到了问题。片段 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.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
我该如何解决这个问题?
在您的TimePicker_Fragmentclass中实施TimePickerDialog.OnTimeSetListener
。
您可以阅读更多关于自定义 TimePicker 的信息 link
在你的异常中阅读这一行。
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 将解决您的问题。