gpt4 book ai didi

java - 设置时间选择器日期

转载 作者:行者123 更新时间:2023-11-30 11:36:19 25 4
gpt4 key购买 nike

编辑:

尝试在日历对象上设置小时和分钟值时出错。

我正在尝试在我的应用程序上设置时间选择器的时间和分钟值。

我将时间值作为字符串存储在我的数据库中。我已将此值解析为 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)

最佳答案

您可以使用 Calendar 从日期对象中获取时间:

Calendar cal = Calendar.getInstance();
cal.setTime( dateToEdit );
timePicker.setCurrentHour( cal.get( HOUR_OF_DAY ) );
timePicker.setCurrentMinute( cal.get( MINUTE ) );

在执行上述操作之前,您必须创建 timePicker,方法是

timePicker = (TimePicker)findViewById( R.id.my_time_picker );

timePicker = new TimePicker( this );

在第二种情况下,您必须以某种方式将其添加到您的布局中。

关于java - 设置时间选择器日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14591771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com