私はカウンター(秒と分をカウントする)を持ち、ディスプレイ上で毎秒更新しようとしています。
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()
。誰かが解決策を知っていますか?