【问题标题】:Why AsyncTask can't implement methods except doInBackground?为什么 AsyncTask 不能实现除 doInBackground 之外的方法?
【发布时间】:2019-05-25 06:36:42
【问题描述】:

在Android Studio中,我尝试实现AsyncTaskmethods接口,但它只显示方法doInBackgound()。我尝试将光标放在 AsyncTask 上,然后按 Alt+Enter,但它只提供方法 doInBackgound()

import android.os.AsyncTask;
import java.net.URL;
public class MyTask extends AsyncTask<URL, Integer, Long> {
    @Override
    protected Long doInBackground(URL... urls) {
        return null;
    }
}

【问题讨论】:

  • 因为它实际上是一个抽象类而不是接口,只有doInBackground是抽象的。

标签: android android-asynctask android-async-http


【解决方案1】:

您只看到AsyncTask::doInBackground(URL...) 的原因是该方法未在抽象类AsyncTask 中实现。

但是,其他的都有默认实现,因此您只能覆盖它们。如果您需要 IntelliJAndroid Studio 来建议其他人,则需要执行 CTRL + O。这将建议您可以从父类覆盖的其他方法。

【讨论】:

  • @steve 通常,当您发现有帮助的答案时,您应该投票和/或接受它作为有效答案
【解决方案2】:

尝试按 Ctrl + O 来覆盖方法。看看是否可以从那里实现 OnPostExecute()。

重要: OnPostExecute() 需要一个从 doInBackground() 返回的参数。如果未提供参数且方法签名不匹配,则覆盖注释可能无法识别该方法。

【讨论】:

    【解决方案3】:
        public class MyTask extends AsyncTask<URL, Integer, Long> {
        @Override
        protected Long doInBackground(URL... urls) {
            return null;
        }
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }
    
        @Override
        protected void onPostExecute(Long aLong) {
            super.onPostExecute(aLong);
        }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-24
      • 1970-01-01
      • 2019-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-07
      相关资源
      最近更新 更多