点击ProgressBar Test按钮后会出现“AsyncTask"已停止运行的错误是为什么呀?
public class ProgressBarTest extends Activity {
private ProgressBar mProgressBar;
private MyAsyncTask mTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.progressbar);
mProgressBar = (ProgressBar) findViewById(R.id.progress);
mTask = new MyAsyncTask();
mTask.execute(); // 启动
}
class MyAsyncTask extends AsyncTask<Void, Integer, Void> {
@Override
protected Void doInBackground(Void... arg0) {
/* for循环模拟进度条的更新 */
for (int i = 0; i < 100; i++) {
if(isCancelled()){
break;
}
publishProgress(i);
try {
Thread.sleep(300);// 增加一个睡眠时间
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
// 获取进度更新值
mProgressBar.setProgress(values[0]);
}
}
}