你好,我试图在每个特定时间更新 textview,但它只更新第一个,然后强制关闭,这是代码:
try {
timer = new Timer();
timerTask = new TimerTask() {
@Override
public void run() {
//Download file here and refresh
updateRecSMSCount(count);
}
};
timer.schedule(timerTask,0, 3000);
} catch (IllegalStateException e){
}
void updateRecSMSCount(Integer count)
{
TextView numRecSMS=(TextView)findViewById(R.id.numRecSMS);
numRecSMS.setText(count.toString());
}
有人可以帮忙吗?
最佳答案
您不能从创建 UI 的线程以外的其他线程访问 UI。定时器任务在不同的线程中运行。您可以在这里找到解决方案:Updating the UI from a Timer
最简单的修复是:
void updateRecSMSCount(Integer count)
{
final TextView numRecSMS=(TextView)findViewById(R.id.numRecSMS);
numRecSMS.post(new Runnable()
{
@Override
public void run()
{
numRecSMS.setText(count.toString());
}
});
}
关于Android:在某个特定的时间间隔更新 textview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6988890/