繁体   English   中英

Android ASyncTask返回类型不匹配

[英]Android ASyncTask Return type mismatch

我正在做一件看起来很简单的事情。 当然这只是一个错字或愚蠢的东西,但我只是没有看到它...

我有一个这样定义的ASyncTask类(并且可以正常编译)

    public class SectionRetrieval extends AsyncTask<Void, Void, List<Section>>
    {


        @Override
        protected List<Section> doInBackground(Void... voids)
        {        
            List<Section> sections = new ArrayList<Section>();

            ...

            return sections;
        }
     }

在我的应用程序中,我尝试这样称呼它

    List<Section> sections = new SectionRetrieval().execute();

但这不能编译

Error:(472, 63) Gradle: error: incompatible types: 

AsyncTask<Void,Void,List<Section>> cannot be converted to List<Section>

我试图将新的SectionalRetrieval实例的创建与调用.execute()分开。 没有这种运气。 似乎确实确实认为execute()确实试图返回SectionalRetrieval的实例。

因此,我如何使其认为是我想要的?

在onPostExecute的参数中,您将从doInBackground返回任何内容。

您也可以通过onPostExecute更新UI。

您可以执行execute().get()进行编译,但我不建议您这样做

您只需执行以下操作即可:

new SectionRetrieval().execute();

此方法不返回任何内容。 这是一个异步任务,因此没有返回值。 任务完成后, onPostExecute将运行,这就是您处理返回值的位置,而不是您调用execute方法的位置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM