无法重写 AsyncTask 类中的 onPostExecute() 方法或使其触发
我在运行 AsyncTask
时无法调用 onPostExecute()
方法。当我尝试设置扩展 AsyncTask
的类(其中覆盖 onPostExecute()
)时,出现以下构建错误。
'AsyncTaskExampleActivity 类型的方法 onPostExecute() 必须 重写或实现超类型方法'
我尝试摆脱 @Override
注释。这消除了构建错误,但该方法仍然不执行。如果有人愿意指出我所忽略的内容,我将不胜感激。
代码:
package com.asynctaskexample;
import android.os.AsyncTask;
public class AsyncTaskExampleActivity extends AsyncTask<Void, Void, Void> {
AsyncTaskExampleActivity(){
super();
}
@Override
protected void onPreExecute() {
}
@Override
protected Void doInBackground(Void... params) {
return null;
}
@Override
protected void onPostExecute() {
}
}
I am having trouble getting the onPostExecute()
method to call when running an AsyncTask
. When I try to set up my class extending AsyncTask
in which the onPostExecute()
is overridden I get the following build error.
'The method onPostExecute() of type AsyncTaskExampleActivity must
override or implement a supertype method'
I have tried getting rid of the @Override
annotation. This gets rid of the build error but the method still does not execute. If any one would be so kind as to point out what I'm overlooking I would greatly appreciated it.
Code:
package com.asynctaskexample;
import android.os.AsyncTask;
public class AsyncTaskExampleActivity extends AsyncTask<Void, Void, Void> {
AsyncTaskExampleActivity(){
super();
}
@Override
protected void onPreExecute() {
}
@Override
protected Void doInBackground(Void... params) {
return null;
}
@Override
protected void onPostExecute() {
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
OnPostExecute()
接受一个参数(从doInBackground()
返回的对象)。将其更改为protected void onPostExecute(Void v)
。如果您不提供参数,则方法签名不匹配,并且覆盖注释开始抱怨没有可以使用此签名覆盖的函数。OnPostExecute()
takes an argument (the object you return fromdoInBackground()
). Change it toprotected void onPostExecute(Void v)
. If you don't provide the argument, the method signatures do not match and the override annotation starts to complain that there is no function to override with this signature.尝试:
在类中尝试右键单击
Source ->覆盖/实现方法..
并查找onPostExecute()
方法。如果它得到的话,它将为您提供包含所有类型参数的完整方法。Try:
In the class try right click
Source -> Override/Implement methods..
and look for theonPostExecute()
method. It will give you complete method with all types of arguments should it get.如果您希望 onPostExecute() 被覆盖,只需使用 doInBackground() 中返回的内容作为 onPostExecute() 中的对象即可。
例如...
if you want your onPostExecute() to be overitten, simply use what was returned in your doInBackground() as an object in your onPostExecute().
For example...
您应该添加 super.onPostExecute() 方法。例如:
编辑:
伙计们,我不知道你为什么拒绝这个答案。提问者缺少
onPostExecute()
方法的参数,并且没有实现超类型方法。这就是我发布这个答案的原因。You should add super.onPostExecute() method. For example:
EDIT:
Guys, I have no idea why you are downvoting the answer. The questioner is missing an argument of
onPostExecute()
method and doesn't have a supertype method implemented. Thats's the reason I posted this answer.