我正在使用this库在我的应用中使用自定义时间选择器。 TimePickerDialog类的相关代码如下:
public static TimePickerDialog newInstance(OnTimeSetListener callback,
int hourOfDay, int minute, boolean is24HourMode) {
TimePickerDialog ret = new TimePickerDialog();
ret.initialize(callback, hourOfDay, minute, is24HourMode);
return ret;
}
在我的活动中,我使用此代码来实现:
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$1 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;
}
};
答案 0 :(得分:2)
您必须使用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);