定时循环的java [英] timed java loop
问题描述
我是新的android,我会AP preciate一些帮助。我有这个code:
dateatm =(的TextView)findViewById(R.id.date);
timeatm =(的TextView)findViewById(R.id.tvTime);
日历的currentdate = Calendar.getInstance();
SimpleDateFormat的格式化=新的SimpleDateFormat(YYYY / MMM / DD);
SimpleDateFormat的瓦克=新的SimpleDateFormat(HH:MM:SS);
串dateNow = formatter.format(currentDate.getTime());
串timeNow =värk.format(currentDate.getTime());
dateatm.setText(dateNow);
timeatm.setText(timeNow);
正如你所看到的,我得到的日期和时间出来。我想寿的是,将有1秒循环,所以每1秒钟后,它会一次又一次更新的日期和时间,以较新的一个拿到基本时钟。我是pretty新人,因此所有帮助AP preciated如何完成这件事。如果您会带来确切的例子我想AP preciate,因为它使得它更容易理解,而不是仅仅哦,使用和和。谢谢你!
感谢您的帮助和时间,
精灵:)
误差答案1:
18 08-10:55:41.335:ERROR / AndroidRuntime(886):致命异常:定时器0
08-10 18:55:41.335:ERROR / AndroidRuntime(886):$ android.view.ViewRoot CalledFromWrongThreadException:只有创建视图层次可以触摸其观点原来的线程。
08-10 18:55:41.335:ERROR / AndroidRuntime(886):在android.view.ViewRoot.checkThread(ViewRoot.java:2932)
08-10 18:55:41.335:ERROR / AndroidRuntime(886):在android.view.ViewRoot.invalidateChild(ViewRoot.java:642)
08-10 18:55:41.335:ERROR / AndroidRuntime(886):在android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:668)
08-10 18:55:41.335:ERROR / AndroidRuntime(886):在android.view.ViewGroup.invalidateChild(ViewGroup.java:2511)
08-10 18:55:41.335:ERROR / AndroidRuntime(886):在android.view.View.invalidate(View.java:5279)
08-10 18:55:41.335:ERROR / AndroidRuntime(886):在android.widget.TextView.checkForRelayout(TextView.java:5507)
08-10 18:55:41.335:ERROR / AndroidRuntime(886):在android.widget.TextView.setText(TextView.java:2724)
08-10 18:55:41.335:ERROR / AndroidRuntime(886):在android.widget.TextView.setText(TextView.java:2592)
08-10 18:55:41.335:ERROR / AndroidRuntime(886):在android.widget.TextView.setText(TextView.java:2567)
08-10 18:55:41.335:ERROR / AndroidRuntime(886):在viimane.voimalus.MainStuff $ 1.run(MainStuff.java:55)
08-10 18:55:41.335:ERROR / AndroidRuntime(886):在java.util.Timer中的$ TimerImpl.run(Timer.java:284)
我刚刚解决了这个问题,在我的项目(执行code。在特定的时间间隔))
编辑,以反映最新的答案:
受保护的静态最终诠释REFRESH = 0;
私人定时器定时器;
私人TimerTask的复习;私人处理程序处理程序=新的处理程序(){
公共无效的handleMessage(消息MSG){
开关(msg.what){
案例刷新:
/ *您code *在这里/
打破;
默认:
打破;
}
}
};
//初始化code中的onCreate或相似的:
定时器=新的Timer();
复习=新的TimerTask(){
公共无效的run(){
ActivityMain.this.sendEmptyMessage(刷新);
};
};
//第一个事件立即以下几点1秒后,每
timer.scheduleAtFixedRate(进修,0,1000);
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)
I just solved this issue in my project (executing code at specific interval) :)
Edited to reflect updated answer:
protected static final int REFRESH = 0;
private Timer timer;
private TimerTask refresher;
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case REFRESH:
/* your code here */
break;
default:
break;
}
}
};
// Initialization code in onCreate or similar:
timer = new Timer();
refresher = new TimerTask() {
public void run() {
ActivityMain.this.sendEmptyMessage(REFRESH);
};
};
// first event immediately, following after 1 seconds each
timer.scheduleAtFixedRate(refresher, 0, 1000);
这篇关于定时循环的java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!