Java 完成 AsyncTask 后未调用 onPostExecute
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3606505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
onPostExecute not called after completion AsyncTask
提问by mlevit
For some reason my onPostExecute()
is not called after my AsyncTask
finishes.
出于某种原因,我onPostExecute()
在AsyncTask
完成后没有被调用。
My class decleration:
我的班级声明:
public class setWallpaperForeground extends AsyncTask<String, Integer, Boolean>
public class setWallpaperForeground extends AsyncTask<String, Integer, Boolean>
My onPostExecute():
我的 onPostExecute():
protected void onPostExecute(Boolean result)
protected void onPostExecute(Boolean result)
Everything works fine, my doInBackground()
completes successfully and returns a Boolean but then it just finishes.
一切正常,我doInBackground()
成功完成并返回一个布尔值,但它刚刚完成。
Thanks
谢谢
采纳答案by Romain Guy
Did you create your AsyncTask on the UI thread? Also add an @Override annotaiton on your onPostExecute() method to make sure you declared it correctly.
您是否在 UI 线程上创建了 AsyncTask?还要在 onPostExecute() 方法上添加 @Override annotaiton 以确保正确声明它。
回答by Konstantin Burov
Did you start the task with execute()
method? The onPostExecute
wouldn't run if you just invoke the doInBackground
.
你用execute()
方法开始任务了吗?的onPostExecute
,如果你只是调用将无法运行doInBackground
。
回答by trichner
Found/Made another nasty mistake:
发现/犯了另一个严重的错误:
If your params of onPostExecute(Param param)
don't match the one you defined with extends AsyncTask<...,...,Param>
and you didn't use the @Override
annotation, it will never be executed and you don't get a warning from Eclipse.
如果您的 paramsonPostExecute(Param param)
与您定义的参数不匹配extends AsyncTask<...,...,Param>
并且您没有使用@Override
注释,它将永远不会被执行并且您不会收到来自 Eclipse 的警告。
Note to myself:
Just always use the @Override
annotation and Eclipse will help you.
对我自己的注意:只要始终使用@Override
注释,Eclipse 就会帮助您。
Another easy way to avoid all named mistakes:
另一种避免所有命名错误的简单方法:
in Eclipse: Right-click in code > Source > Override/Implement Methods
在 Eclipse 中: Right-click in code > Source > Override/Implement Methods
回答by Hadas Kaminsky
After having the same problem and none of these answers helped me, I found out that my UI thread was blocked (I used a CountDownLatch.await()
) and therefore the onPostExecute()
method that is supposed to be called by the UI thread was never called.
在遇到同样的问题并且这些答案都没有帮助我之后,我发现我的 UI 线程被阻塞了(我使用了 a CountDownLatch.await()
),因此onPostExecute()
应该由 UI 线程调用的方法从未被调用过。
回答by marcinj
I had the same behaviour, and the cause was that I have been posting a lot of messages as a progress inside doInBackground with following code:
我有同样的行为,原因是我在 doInBackground 中使用以下代码发布了大量消息作为进度:
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
// .. some UI updates
}
});
this must have overloaded main thrad message queue, and caused long delay before onPostExecute would get called. The solution was to post only once every second.
这一定是重载了主线程消息队列,并在 onPostExecute 被调用之前导致了长时间的延迟。解决方案是每秒只发布一次。
回答by Anarchofascist
Made another nasty mistake that can result in this same error. When defining the AsyncTask and calling it, I was not calling execute
but was calling doInBackground
犯了另一个严重的错误,可能导致同样的错误。在定义 AsyncTask 并调用它时,我不是在调用execute
而是在调用doInBackground
new AsyncTask<String,Void,Void>() {
....
}.doInBackground("parameter");
rather than
而不是
new AsyncTask<String,Void,Void>() {
....
}.execute("parameter");
回答by AlanKley
For me it was user error. I was ending the AsyncTask by invoking cancel(true) on it and not reading the documentation closely enough to know that onPostExecute is not called in this case, onCancelled is.
对我来说,这是用户错误。我通过调用 cancel(true) 来结束 AsyncTask 并且没有足够仔细地阅读文档以知道在这种情况下没有调用 onPostExecute ,而 onCancelled 是。
回答by oiyio
I have faced the same problem. None of the above solutions worked for me. Then i figured out the problem maybe it helps someone else .
我遇到了同样的问题。以上解决方案都不适合我。然后我想通了这个问题,也许它可以帮助其他人。
In UI thread i call the following codes:
在 UI 线程中,我调用以下代码:
public class XActivity ...{
onCreate(){
....
new SaveDrawingAsync(this).execute();
while(true)
{
if(MandalaActivity.saveOperationInProgress){
continue;
}
super.onBackPressed();
break;
}
...
}
}
My AsyncTask class definition :
我的 AsyncTask 类定义:
public class SaveAsync extends AsyncTask<Object, Void, Void> {
@Override
public Void doInBackground(Object... params) {
saveThem(); // long running operation
return null;
}
@Override
public void onPostExecute(Void param) {
XActivity.saveOperationInProgress = false;
}
@Override
public void onPreExecute() {
XActivity.saveOperationInProgress = true;
}
}
in the above code onPostExecute is not called. It is because of an infinite loop after asynctask execution . asynctask and inifinite loop both waits eachother to finish. Thus the code stucks!
在上面的代码中 onPostExecute 没有被调用。这是因为 asynctask 执行后的无限循环。异步任务和无限循环都等待对方完成。因此代码卡住了!
The solution is changing the design!
解决方案是改变设计!