【发布时间】:2010-12-04 01:14:51
【问题描述】:
我正在尝试有一个计数器(计数秒和分钟)并每秒在显示屏上更新它。
我在班级的onCreate 中有这段代码,它扩展了Activity:
timeOnCall = (TextView) findViewById(R.id.time);
minutes = seconds = 0;
timeOnCall.setText(minutes + ":" + seconds);
// Implements the timer
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
++seconds;
if (seconds == 60) {
seconds = 0;
++minutes;
}
// Display the new time
timeOnCall.setText(minutes + ":" + seconds);
}
}, 1000, 1000);
很遗憾,我收到以下错误:
android.view.ViewRoot$CalledFromWrongThreadException: 只有创建视图层次结构的原始thread 才能触及其视图。
我不确定如何解决这个问题,因为它已经在 onCreate() 方法中。有谁知道解决办法吗?
【问题讨论】:
标签: android xml textview timertask