我試圖在每半秒運行一次for循環。每次調用循環時都會改變視圖中的某些內容,但是這些改變是由另一個類別即速度表完成的。Android「只有創建視圖層次結構的原始線程可以觸及其視圖。」for循環中的錯誤
Thread thread=new Thread(){
@Override
public void run() {
float i;
try {
for (i = 0; i <= 100; i++) {
Speedometer1.onSpeedChanged(Speedometer1.getCurrentSpeed(i) + 8);
Speedometer2.onSpeedChanged(Speedometer2.getCurrentSpeed(i) + 8);
Speedometer3.onSpeedChanged(Speedometer3.getCurrentSpeed(i) + 8);
Speedometer4.onSpeedChanged(Speedometer4.getCurrentSpeed(i) + 8);
Speedometer5.onSpeedChanged(Speedometer5.getCurrentSpeed(i) + 8);
Speedometer6.onSpeedChanged(Speedometer6.getCurrentSpeed(i) + 8);
sleep(500);
}
}
catch (InterruptedException e)
{e.printStackTrace();}
}
};thread.start();
參考[安卓「只有創建視圖層次結構的原始線程可以觸及其視圖。「](http://stackoverflow.com/questions/5161951/android-only-the-original-thread-that-created-a-view-hierarchy-can -touch-its-vi) –