我想在Android中制作一個(gè)chronometer計(jì)時(shí)器,使用了timer和sheduleAtFixedRate 方法??雌饋?lái)像是在timer的run方法中調(diào)用textview后,程序就會(huì)出錯(cuò)停止運(yùn)行。我用的是以下的代碼,哪里出錯(cuò)了呢?Button boton_iniciar;
TextView texto_cronometro;
Timer count;
int a = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cronometro);
/**********************/
boton_iniciar = (Button) findViewById(R.id.button1);
texto_cronometro = (TextView) findViewById(R.id.textView1);
count= new Timer("Contador");
boton_iniciar.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
count.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
a++;
texto_cronometro.setText(String.valueOf(a));
}
}, 100, 100);
}
});
}
3 回答

藍(lán)山帝景
TA貢獻(xiàn)1843條經(jīng)驗(yàn) 獲得超7個(gè)贊
更新UI線程之外的用戶界面。
用這些代碼:
<youractivityname>.this.runOnUiThread(new Runnable() { public void run() { texto_cronometro.setText(String.valueOf(a)); } });
代替
texto_cronometro.setText(String.valueOf(a));
添加回答
舉報(bào)
0/150
提交
取消