我和androïd一起工作了几个星期。 我正在尝试使用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
...
undefinitly。所以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());
}
答案 0 :(得分:2)
你的AsyncTask永远不会完成,因为你没有给它机会甚至开始。
当你调用execute时,循环会准备doInBackground的调度。但是你在主线程上循环和睡眠,永远不会从onClick返回,永远不会让looper有机会在后台线程上安排doInBackground ..
你必须在后台线程上安排doInBackground之前返回。
或者你已经有10个asynctasks卡住了。
不完全,但想法一致。
答案 1 :(得分:2)
永远不会调用onPostExecute()的原因是你在主线程中休眠,并且由于onPostExecute()在主线程上运行,你永远不会允许它被执行。
这应该有效。我还清理了一些代码:
public class MainActivity extends Activity {
private AsyncClic clic=new AsyncClic();
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
Button bActiver = (Button) 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 ! ");
}
@Override
protected Integer doInBackground(Void... arg0) {
Log.d(TAG, "AsyncClic");
return err;
}
}
class bActiverListener implements OnClickListener {
@Override
public void onClick(View v) {
clic.execute();
}
}
}