TextView を毎秒更新するシンプルなタイマーを Android で作成したいと考えています。マインスイーパのように秒数をカウントするだけです。
問題は、tvTime.setText(...) を無視するときです (//tvTime.setText(...) にします。LogCat では、毎秒次の番号が出力されます。しかし、この番号をTextView (別のスレッドで作成)、プログラムがクラッシュします。
これを簡単に解決する方法を知っている人はいますか?
コードは次のとおりです (メソッドは起動時に呼び出されます)。
private void startTimerThread() {
Thread th = new Thread(new Runnable() {
private long startTime = System.currentTimeMillis();
public void run() {
while (gameState == GameState.Playing) {
System.out.println((System.currentTimeMillis() - this.startTime) / 1000);
tvTime.setText("" + ((System.currentTimeMillis() - this.startTime) / 1000));
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
th.start();
}
編集:
ついにできた。これが解決策です。興味のある人向けです。
private void startTimerThread() {
Thread th = new Thread(new Runnable() {
private long startTime = System.currentTimeMillis();
public void run() {
while (gameState == GameState.Playing) {
runOnUiThread(new Runnable() {
@Override
public void run() {
tvTime.setText(""+((System.currentTimeMillis()-startTime)/1000));
}
});
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
th.start();
}