编辑:
尝试在日历对象上设置小时和分钟值时出错。
我正在尝试在我的应用程序上设置时间选择器的时间和分钟值。
我将时间值作为字符串存储在我的数据库中。我已将此值解析为 Date 对象,现在我不确定如何获取小时和分钟值来设置选择器本身。
即,如果时间存储在晚上 8.30,我想通过我班级的 onCreate 方法在时间选择器上设置它。
到目前为止,这是我的代码:
DateFormat newFormat = new SimpleDateFormat("hh:mm");
try
{
dateToEdit = newFormat.parse(returnedTime);
} catch (ParseException e)
{
e.printStackTrace();
}
Calendar cal = Calendar.getInstance();
cal.setTime(dateToEdit);
editTimePicker.setCurrentHour(Calendar.HOUR_OF_DAY);
editTimePicker.setCurrentMinute(Calendar.MINUTE);
日志猫显示错误
01-29 21:58:47.860: D/AndroidRuntime(269): Shutting down VM
01-29 21:58:47.870: W/dalvikvm(269): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
01-29 21:58:47.890: E/AndroidRuntime(269): FATAL EXCEPTION: main
01-29 21:58:47.890: E/AndroidRuntime(269): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.flybase2/com.example.flybase2.AppointmentEditChanges}: java.lang.NullPointerException
01-29 21:58:47.890: E/AndroidRuntime(269): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-29 21:58:47.890: E/AndroidRuntime(269): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-29 21:58:47.890: E/AndroidRuntime(269): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-29 21:58:47.890: E/AndroidRuntime(269): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-29 21:58:47.890: E/AndroidRuntime(269): at android.os.Handler.dispatchMessage(Handler.java:99)
01-29 21:58:47.890: E/AndroidRuntime(269): at android.os.Looper.loop(Looper.java:123)
01-29 21:58:47.890: E/AndroidRuntime(269): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-29 21:58:47.890: E/AndroidRuntime(269): at java.lang.reflect.Method.invokeNative(Native Method)
01-29 21:58:47.890: E/AndroidRuntime(269): at java.lang.reflect.Method.invoke(Method.java:521)
01-29 21:58:47.890: E/AndroidRuntime(269): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-29 21:58:47.890: E/AndroidRuntime(269): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-29 21:58:47.890: E/AndroidRuntime(269): at dalvik.system.NativeStart.main(Native Method)
01-29 21:58:47.890: E/AndroidRuntime(269): Caused by: java.lang.NullPointerException
01-29 21:58:47.890: E/AndroidRuntime(269): at com.example.flybase2.AppointmentEditChanges.onCreate(AppointmentEditChanges.java:100)
01-29 21:58:47.890: E/AndroidRuntime(269): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-29 21:58:47.890: E/AndroidRuntime(269): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)