无法重写 AsyncTask 类中的 onPostExecute() 方法或使其触发

发布于 2024-12-04 02:52:29 字数 742 浏览 3 评论 0原文

我在运行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

始终不够爱げ你 2024-12-11 02:52:29

OnPostExecute() 接受一个参数(从 doInBackground() 返回的对象)。将其更改为 protected void onPostExecute(Void v)。如果您不提供参数,则方法签名不匹配,并且覆盖注释开始抱怨没有可以使用此签名覆盖的函数。

OnPostExecute() takes an argument (the object you return from doInBackground()). Change it to protected 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.

逆蝶 2024-12-11 02:52:29

尝试:

在类中尝试右键单击 Source ->覆盖/实现方法.. 并查找 onPostExecute() 方法。如果它得到的话,它将为您提供包含所有类型参数的完整方法。

Try:

In the class try right click Source -> Override/Implement methods.. and look for the onPostExecute() method. It will give you complete method with all types of arguments should it get.

何以心动 2024-12-11 02:52:29

如果您希望 onPostExecute() 被覆盖,只需使用 doInBackground() 中返回的内容作为 onPostExecute() 中的对象即可。

例如...

    @Override
    protected void doInBackground(Void...args0){
        // your code here...
        return value;
    }
    @Override
    protected void onPostExecute(void value){
        //code to run
    }

if you want your onPostExecute() to be overitten, simply use what was returned in your doInBackground() as an object in your onPostExecute().

For example...

    @Override
    protected void doInBackground(Void...args0){
        // your code here...
        return value;
    }
    @Override
    protected void onPostExecute(void value){
        //code to run
    }
天煞孤星 2024-12-11 02:52:29

您应该添加 super.onPostExecute() 方法。例如:

@Override
protected void onPostExecute(Void nothing)
{
   super.onPostExecute(nothing);
}

编辑:
伙计们,我不知道你为什么拒绝这个答案。提问者缺少 onPostExecute() 方法的参数,并且没有实现超类型方法。这就是我发布这个答案的原因。

You should add super.onPostExecute() method. For example:

@Override
protected void onPostExecute(Void nothing)
{
   super.onPostExecute(nothing);
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文