显示自定义时间选择器期间应用强制关闭
App force closes during displaying custom time picker
我正在使用 this 库在我的应用程序中使用自定义时间选择器。来自TimePickerDialog的相关代码class如下:
public static TimePickerDialog newInstance(OnTimeSetListener callback,
int hourOfDay, int minute, boolean is24HourMode) {
TimePickerDialog ret = new TimePickerDialog();
ret.initialize(callback, hourOfDay, minute, is24HourMode);
return ret;
}
在我的 activity 中,我使用这段代码来实现:
com.android.datetimepicker.time.TimePickerDialog dialog = new com.android.datetimepicker.time.TimePickerDialog();
dialog.newInstance((OnTimeSetListener) mTimeSetListener, getHour, getMinute, true);
FragmentManager fragmentManager;
fragmentManager = getSupportFragmentManager();
dialog.show(fragmentManager, "timepicker");
但是当我启动应用程序时,我强制关闭并出现以下错误:
01-10 11:08:29.781 8727-8727/com.example.unplugged E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.ClassCastException: com.example.unplugged.MainActivity cannot be cast to com.android.datetimepicker.time.TimePickerDialog$OnTimeSetListener
at com.example.unplugged.MainActivity.onClick(MainActivity.java:48)
at android.view.View.performClick(View.java:4249)
at android.view.View$PerformClick.run(View.java:17764)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5162)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:756)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:572)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
有什么建议吗?
编辑:回调方法:
private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener()
{
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
setHour = hourOfDay;
setMinute = minute;
}
};
您必须使用 android.app.TimePickerDialog.OnTimeSetListener
改变
dialog.newInstance((OnTimeSetListener) mTimeSetListener, getHour, getMinute, true);
至
dialog.newInstance(new com.android.datetimepicker.time.TimePickerDialog.OnTimeSetListener()
{
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
setHour = hourOfDay;
setMinute = minute;
}
}, getHour, getMinute, true);
我正在使用 this 库在我的应用程序中使用自定义时间选择器。来自TimePickerDialog的相关代码class如下:
public static TimePickerDialog newInstance(OnTimeSetListener callback,
int hourOfDay, int minute, boolean is24HourMode) {
TimePickerDialog ret = new TimePickerDialog();
ret.initialize(callback, hourOfDay, minute, is24HourMode);
return ret;
}
在我的 activity 中,我使用这段代码来实现:
com.android.datetimepicker.time.TimePickerDialog dialog = new com.android.datetimepicker.time.TimePickerDialog();
dialog.newInstance((OnTimeSetListener) mTimeSetListener, getHour, getMinute, true);
FragmentManager fragmentManager;
fragmentManager = getSupportFragmentManager();
dialog.show(fragmentManager, "timepicker");
但是当我启动应用程序时,我强制关闭并出现以下错误:
01-10 11:08:29.781 8727-8727/com.example.unplugged E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.ClassCastException: com.example.unplugged.MainActivity cannot be cast to com.android.datetimepicker.time.TimePickerDialog$OnTimeSetListener
at com.example.unplugged.MainActivity.onClick(MainActivity.java:48)
at android.view.View.performClick(View.java:4249)
at android.view.View$PerformClick.run(View.java:17764)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5162)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:756)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:572)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
有什么建议吗?
编辑:回调方法:
private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener()
{
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
setHour = hourOfDay;
setMinute = minute;
}
};
您必须使用 android.app.TimePickerDialog.OnTimeSetListener
改变
dialog.newInstance((OnTimeSetListener) mTimeSetListener, getHour, getMinute, true);
至
dialog.newInstance(new com.android.datetimepicker.time.TimePickerDialog.OnTimeSetListener()
{
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
setHour = hourOfDay;
setMinute = minute;
}
}, getHour, getMinute, true);