我试图在重置游戏之前引入1秒暂停(resetGame()).按下按钮后. bAnswer1文本等于ansewrArray [0].在newQuestionTimer()中设置的1秒延迟后,App力会关闭.
import java.util.Timer;
import java.util.TimerTask;
Timer timer = new Timer();
bAnswer1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(bAnswer1.getText().toString().equals(answerArray[0]))
{
bAnswer1.setBackgroundColor(Color.GREEN);
newQuestionTimer();
}
else
{
bAnswer1.setBackgroundColor(Color.RED);
guess++;
}
}
});
public void newQuestionTimer()
{
timer.schedule(new TimerTask() {
@Override
public void run() {
resetGame();
}
},1000);
}
最佳答案
您正在从在后台线程上运行的计时器更新ui. Ui只能在ui线程上更新.
原文链接:https://www.f2er.com/android/431051.html您可以使用处理程序
Handler handler = new Handler();
handler.postDelayed(new Runnable(){
@Override
public void run() {
bAnswer2.setBackgroundColor(Color.TRANSPARENT);
bAnswer3.setBackgroundColor(Color.TRANSPARENT);
bAnswer4.setBackgroundColor(Color.TRANSPARENT);
}
},1000);