如何改变在Android的新的TimerTask动作的东西吗? [英] how to change something in a new TimerTask action in android?

查看:141
本文介绍了如何改变在Android的新的TimerTask动作的东西吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TimerTask在Android中,我有一个code这样的问题:

i have a problem with the timerTask in android i have a code like this:

timer = new Timer();
timer.schedule(new TimerTask() {
        public void run() {
            countInt = countInt + 1;
            textview1.setText(countInt);
        }
    }, 1000);

每一次的计时器任务获得startet我的应用程序崩溃,我的事情,因为我访问的TextView,这是在其他线程吧?

every time the timer task get startet my app crashed, i thing because i'm accessing the textview and it is in a other thread right?

如何解决此问题?

推荐答案

试试这个..

 timer = new Timer();
    timer.schedule(new TimerTask() {
            public void run() {
                countInt = countInt + 1;
                yourActivity.this.runOnUiThread(new Runnable()
               public void run(){
                  {textview1.setText(String.valueOf(countInt))});
                }
            }
        }, 1000);

它崩溃,因为你的东西搞乱(textview1.setText(countInt);)属于这是不允许的UI线程...

It crashes because you are messing with something ( textview1.setText(countInt);) that belongs UI thread which is not allowed...

这篇关于如何改变在Android的新的TimerTask动作的东西吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆