我有一个问题,我无法在倒计时期间设置按钮的文本,这是代码:
begin.setText("Start level 1");
begin.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
final int delay = 0000; // delay for 5 sec.
final int period = 1000; // repeat every sec.
final Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
begin.setText("" + t);
t--;
if(t == 0) {
timer.cancel()
}
}
}, delay, period);
这是日志猫:
android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
07-30 17:27:05.273: E/AndroidRuntime(22543): at android.view.ViewRoot.checkThread(ViewRoot.java:2802)
07-30 17:27:05.273: E/AndroidRuntime(22543): at android.view.ViewRoot.requestLayout(ViewRoot.java:594)
07-30 17:27:05.273: E/AndroidRuntime(22543): at android.view.View.requestLayout(View.java:8128)
07-30 17:27:05.273: E/AndroidRuntime(22543): at android.view.View.requestLayout(View.java:8128)
07-30 17:27:05.273: E/AndroidRuntime(22543): at android.view.View.requestLayout(View.java:8128)
07-30 17:27:05.273: E/AndroidRuntime(22543): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:254)
07-30 17:27:05.273: E/AndroidRuntime(22543): at android.view.View.requestLayout(View.java:8128)
07-30 17:27:05.273: E/AndroidRuntime(22543): at android.view.View.requestLayout(View.java:8128)
07-30 17:27:05.273: E/AndroidRuntime(22543): at android.widget.TextView.checkForRelayout(TextView.java:5405)
07-30 17:27:05.273: E/AndroidRuntime(22543): at android.widget.TextView.setText(TextView.java:2712)
07-30 17:27:05.273: E/AndroidRuntime(22543): at android.widget.TextView.setText(TextView.java:2580)
07-30 17:27:05.273: E/AndroidRuntime(22543): at android.widget.TextView.setText(TextView.java:2555)
07-30 17:27:05.273: E/AndroidRuntime(22543): at it.bisemanuDEV.piano.livello1$1$1.run(livello1.java:134)
07-30 17:27:05.273: E/AndroidRuntime(22543): at java.util.Timer$TimerImpl.run(Timer.java:289)
我希望你能帮忙。谢谢