无法停止/启动的AsyncTask [英] Can't stop/restart AsyncTask
本文介绍了无法停止/启动的AsyncTask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请帮忙。我可以重新启动AsyncTask的。应用程序崩溃每一次,当第二次调用updatePoi()。
下面是我的code:
-
我检查任务的状态,并设置取消(真)。
公共无效updatePoi(){
//新RefreshMapTask()执行();
如果(refreshMapTask.getStatus()== AsyncTask.Status.RUNNING ||
refreshMapTask.getStatus()== AsyncTask.Status.PENDING){
refreshMapTask.cancel(真);
}
refreshMapTask.execute();
}
} -
下面是我的AsyncTask。在doInBackground我写了一个突破。
私有类RefreshMapTask扩展的AsyncTask<太虚,太虚,太虚> {
@覆盖
在preExecute保护无效(){
super.on preExecute();
getMapView()getOverlays()清()。
myPoiOverlay.clear();
exitOverlay.clear();
}
@覆盖
保护无效doInBackground(虚空......空隙){
应用程序=(应用程序)getApplication();
Log.d(TAG,退出数=+ app.getExits()大小()); GeoPoint的pointToNavigate = NULL; 对于(EXIT退出:app.getExits()){ 为(POI的POI:exit.getPoi()){
如果(isCancelled()){
打破;
}
//一些code在这里
}
} //小code在这里
返回null;
} @覆盖
保护无效onPostExecute(虚空避免){
getMapView()无效()。
}
}
解决方案
这是的AsyncTask
实例只能被调用一次。为了使第二个电话,你需要创建一个新的实例。
Please help. I can restart the AsyncTask. App crashes every time, when second call to updatePoi().
Here is my code:
I'm checking status of task and set cancel(true).
public void updatePoi() { //new RefreshMapTask().execute(); if (refreshMapTask.getStatus() == AsyncTask.Status.RUNNING || refreshMapTask.getStatus() == AsyncTask.Status.PENDING) { refreshMapTask.cancel(true); } refreshMapTask.execute(); } }
Here is my AsyncTask. in doInBackground I wrote a break.
private class RefreshMapTask extends AsyncTask<Void, Void, Void> { @Override protected void onPreExecute() { super.onPreExecute(); getMapView().getOverlays().clear(); myPoiOverlay.clear(); exitOverlay.clear(); } @Override protected Void doInBackground(Void... voids) { Application app = (Application)getApplication(); Log.d(TAG, "exits count = " + app.getExits().size()); GeoPoint pointToNavigate = null; for (Exit exit : app.getExits()) { for (Poi poi : exit.getPoi()) { if (isCancelled()){ break; } //some code here } } //small code here return null; } @Override protected void onPostExecute(Void aVoid) { getMapView().invalidate(); } }
解决方案
An AsyncTask
instance can only be called once. To make a second call, you need to create a new instance.
这篇关于无法停止/启动的AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文