【发布时间】:2011-08-02 15:22:42
【问题描述】:
我有一个活动(实际上是 MapActivity),它有一个带有 mapView 和一个 textView 的 linearLayout,用于显示当前速度等。例如,我希望 textView 每 0.5 秒更新一次。
我知道这可以通过服务来完成(至少我是这样学会的),但我想知道是否可以在 MapActivity 本身中使用 Timer。我试过这种方法:
onCreate{
...
updateTimer = new Timer();
updateTimer.scheduleAtFixedRate(doRefresh, 0, updateInterval);
}
private Timer updateTimer;
private TimerTask doRefresh = new TimerTask()
{
public void run()
{
updateData();
}
};
private void updateData()
{
//update the textView with the data
}
private int updateInterval = 500;
但是,它给了我以下错误:
04-10 22:24:56.529: ERROR/AndroidRuntime(9434): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
那么,是否可以在不使用服务类的情况下以简单的方式完成我正在尝试的事情?
谢谢和问候, =)
【问题讨论】:
标签: android android-activity scheduled-tasks