定时java循环
我是新的机器人,希望得到一些帮助。我有这段代码:
dateatm = (TextView) findViewById(R.id.date);
timeatm = (TextView) findViewById(R.id.tvTime);
Calendar currentDate = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MMM/dd");
SimpleDateFormat värk = new SimpleDateFormat("HH:mm:ss");
String dateNow = formatter.format(currentDate.getTime());
String timeNow = värk.format(currentDate.getTime());
dateatm.setText(dateNow);
timeatm.setText(timeNow);
如您所见,我正在从中获取日期和时间。我想要的是,会有 1 秒循环,所以每 1 秒后,它将一次又一次更新日期和时间到较新的日期和时间以获得基本时钟。我是个新人,所以感谢所有帮助如何完成这项工作。如果您能提供确切的示例,我将不胜感激,因为它使它更容易理解,而不仅仅是“哦,使用那个、那个、那个。谢谢!”。
感谢您的帮助和时间, Elven :)
答案 1 错误:
08-10 18:55:41.335: ERROR/AndroidRuntime(886): FATAL EXCEPTION: Timer-0
08-10 18:55:41.335: ERROR/AndroidRuntime(886): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
08-10 18:55:41.335: ERROR/AndroidRuntime(886): at android.view.ViewRoot.checkThread(ViewRoot.java:2932)
08-10 18:55:41.335: ERROR/AndroidRuntime(886): at android.view.ViewRoot.invalidateChild(ViewRoot.java:642)
08-10 18:55:41.335: ERROR/AndroidRuntime(886): at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:668)
08-10 18:55:41.335: ERROR/AndroidRuntime(886): at android.view.ViewGroup.invalidateChild(ViewGroup.java:2511)
08-10 18:55:41.335: ERROR/AndroidRuntime(886): at android.view.View.invalidate(View.java:5279)
08-10 18:55:41.335: ERROR/AndroidRuntime(886): at android.widget.TextView.checkForRelayout(TextView.java:5507)
08-10 18:55:41.335: ERROR/AndroidRuntime(886): at android.widget.TextView.setText(TextView.java:2724)
08-10 18:55:41.335: ERROR/AndroidRuntime(886): at android.widget.TextView.setText(TextView.java:2592)
08-10 18:55:41.335: ERROR/AndroidRuntime(886): at android.widget.TextView.setText(TextView.java:2567)
08-10 18:55:41.335: ERROR/AndroidRuntime(886): at viimane.voimalus.MainStuff$1.run(MainStuff.java:55)
08-10 18:55:41.335: ERROR/AndroidRuntime(886): at java.util.Timer$TimerImpl.run(Timer.java:284)
I am new android and I would appreciate some help. I have this code:
dateatm = (TextView) findViewById(R.id.date);
timeatm = (TextView) findViewById(R.id.tvTime);
Calendar currentDate = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MMM/dd");
SimpleDateFormat värk = new SimpleDateFormat("HH:mm:ss");
String dateNow = formatter.format(currentDate.getTime());
String timeNow = värk.format(currentDate.getTime());
dateatm.setText(dateNow);
timeatm.setText(timeNow);
As you can see, I am getting date and time out of it. What I want tho is that there would be 1 second loop, so after every 1 second, it will update date and time again and again to the newer one to get basically clock. I am pretty newcomer so all help is appreciated how to get this done. If you would bring exact examples I would appreciate as it makes it more easier to understand, not just "Oh, use that and that and that. Thank you!".
Thanks for your help and time,
Elven :)
Error for answer 1:
08-10 18:55:41.335: ERROR/AndroidRuntime(886): FATAL EXCEPTION: Timer-0
08-10 18:55:41.335: ERROR/AndroidRuntime(886): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
08-10 18:55:41.335: ERROR/AndroidRuntime(886): at android.view.ViewRoot.checkThread(ViewRoot.java:2932)
08-10 18:55:41.335: ERROR/AndroidRuntime(886): at android.view.ViewRoot.invalidateChild(ViewRoot.java:642)
08-10 18:55:41.335: ERROR/AndroidRuntime(886): at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:668)
08-10 18:55:41.335: ERROR/AndroidRuntime(886): at android.view.ViewGroup.invalidateChild(ViewGroup.java:2511)
08-10 18:55:41.335: ERROR/AndroidRuntime(886): at android.view.View.invalidate(View.java:5279)
08-10 18:55:41.335: ERROR/AndroidRuntime(886): at android.widget.TextView.checkForRelayout(TextView.java:5507)
08-10 18:55:41.335: ERROR/AndroidRuntime(886): at android.widget.TextView.setText(TextView.java:2724)
08-10 18:55:41.335: ERROR/AndroidRuntime(886): at android.widget.TextView.setText(TextView.java:2592)
08-10 18:55:41.335: ERROR/AndroidRuntime(886): at android.widget.TextView.setText(TextView.java:2567)
08-10 18:55:41.335: ERROR/AndroidRuntime(886): at viimane.voimalus.MainStuff$1.run(MainStuff.java:55)
08-10 18:55:41.335: ERROR/AndroidRuntime(886): at java.util.Timer$TimerImpl.run(Timer.java:284)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我刚刚在我的项目中解决了这个问题(以特定的时间间隔执行代码):)
编辑以反映更新的答案:
// onCreate 或类似中的初始化代码:
I just solved this issue in my project (executing code at specific interval) :)
Edited to reflect updated answer:
// Initialization code in onCreate or similar: