我只和 android 一起工作了几个星期。我正在尝试使用 AsyncTask。问题是我的 onPostExecute 从未被调用,并且 AsyncTask 似乎永远不会完成。
我只是在日志文件中得到这个:
04-04 11:47:42.722: D/BbHelper(10343): AsyncClic PreExecute !
04-04 11:47:42.742: D/BbHelper(10343): AsyncClic
04-04 11:47:43.774: D/BbHelper(10343): Waiting... clic=RUNNING
04-04 11:47:44.838: D/BbHelper(10343): Waiting... clic=RUNNING
04-04 11:47:45.869: D/BbHelper(10343): Waiting... clic=RUNNING
04-04 11:47:46.919: D/BbHelper(10343): Waiting... clic=RUNNING
04-04 11:47:47.977: D/BbHelper(10343): Waiting... clic=RUNNING
04-04 11:47:48.999: D/BbHelper(10343): Waiting... clic=RUNNING
04-04 11:47:50.001: D/BbHelper(10343): Waiting... clic=RUNNING
04-04 11:47:51.005: D/BbHelper(10343): Waiting... clic=RUNNING
...
不确定。所以 getStatus 总是返回 RUNNING。
因此 AsyncTask 已启动,但从未完成。
我的代码有什么问题?
提前致谢。安德烈
public class MainActivity extends Activity {
private AsyncClic clic;
clic=new AsyncClic();
@Override
protected void onCreate(Bundle savedInstanceState) {
layout = (LinearLayout) LinearLayout.inflate(this, R.layout.activity_main, null);
setContentView(layout);
bActiver = (Button) layout.findViewById(R.id.bActiver);
bActiver.setOnClickListener(new bActiverListener());
}
private class AsyncClic extends AsyncTask<Void, Void, Integer>{
public Integer err = -1;
@Override
protected void onPreExecute() {
Log.d(TAG, "AsyncClic PreExecute ! ");
}
@Override
protected void onPostExecute(Integer result) {
Log.d(TAG, "AsyncClic PostExecute ! ");
//progressDialog.dismiss();
}
@Override
protected Integer doInBackground(Void... arg0) {
Log.d(TAG, "AsyncClic");
return err;
}
}
class bActiverListener implements OnClickListener{
@Override
public void onClick(View arg0) {
clic.execute();
while(!clic.getStatus().equals(AsyncTask.Status.FINISHED)){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d(TAG, "Waiting... clic=" + clic.getStatus());
}