我只想在设定的间隔(2秒)内更改图像视图的位图图像
我试过这个,但应用程序崩溃了:
private void prefromRadarInterval() {
int delay = 1000; // delay for 0 sec.
int period = 1000; // repeat every 1 seconds.
timer = new Timer();
timer.scheduleAtFixedRate(new SampleTimerTask(), delay, period);
}
public class SampleTimerTask extends TimerTask {
@Override
public void run() {
//MAKE YOUR LOGIC TO SET IMAGE TO IMAGEVIEW
imageview_radarcurrent.setImageBitmap(radar_animation[flag]);
flag++;
if(flag > 9) {
flag = 0;
}
}
}
log cat打印出来:
01-12 04:51:51.688: E/AndroidRuntime(19688): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
帮助和解释将不胜感激!
答案 0 :(得分:0)
你的问题是UI只能被修改购买UI线程,你的TimerTask正在它自己的线程上运行。解决这个问题的最简单方法可能是通过处理程序发布到UI线程。
答案 1 :(得分:0)
您应该从UI线程调用setImageBitmap()。 例如:
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
// Write your code here
}
});
...或者用Runnable发布它:
imageview_radarcurrent.post(new Runnable() {
@Override
public void run() {
}
});