我试图在我的Asynctask中添加一些睡眠时间,因为当没有太多数据需要加载时,我的ProgressDialog太快了。
我试过了:
@Override
protected Boolean doInBackground(Void... params) {
try {
progressDialog.setMessage("Loading first thing...");
firstThing();
progressDialog.incrementProgressBy(1);
Thread.sleep(500);
//...repeat above four lines a few times for second, third, fourth thing, etc
return true;
}
catch (Exception e) {
Log.e("MyClassName", "There was an error: " + e);
return false;
}
}
我收到错误“只有创建视图的原始线程才能触及其视图。”
答案 0 :(得分:2)
您必须覆盖onProgressUpdate()
以及doInBackground()
。
// do this before asynctask.execute();
progressDialog.setMessage("Loading first thing...");
@Override
protected Boolean doInBackground(Void... params) {
try {
firstThing();
Thread.sleep(500);
// this method invokes onProgressUpdate on the UI thread
publishProgress();
return true;
}
catch (Exception e) {
Log.e("MyClassName", "There was an error: " + e);
return false;
}
}
@Override
protected void onProgressUpdate(Void... params) {
progressDialog.incrementProgressBy(1);
}