EDIT:
Error trying to set hour and minute values on the calender object.
I’m trying to set the time and minute values of a time picker on my app.
I have the time value stored as a string in my database. I have parsed this value to a Date object and now I’m not exactly sure how I can get the hour and minute value to set the picker itself.
I.e if the time was stored at 8.30 PM, I want to set this on the time picker through the onCreate method of my class.
Here’s my code so far:
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);
Log cat showing errors
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)
You can use a
Calendarto get the time out of the date object:You’ll have to create
timePickerbefore doing the above, either byor
in the second case, you’ll then have to add it to your layout somehow.