定时java循环

发布于 2024-11-29 02:07:17 字数 2232 浏览 0 评论 0原文

我是新的机器人,希望得到一些帮助。我有这段代码:

        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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甲如呢乙后呢 2024-12-06 02:07:17

我刚刚在我的项目中解决了这个问题(以特定的时间间隔执行代码):)

编辑以反映更新的答案:

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;
        }
    }
};

// onCreate 或类似中的初始化代码:

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); 

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); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文