【问题标题】:why android studio giving error when i remove null from function?当我从函数中删除 null 时,为什么 android studio 会出错?
【发布时间】:2015-08-18 08:34:40
【问题描述】:

我是 android 开发的新手。我正在使用 asynctask 方法来调用我的函数。一切正常,但我不知道为什么函数会返回数据以及为什么 android studio 会给出这个错误

> Error:(182, 9) error: missing return statement

当我删除return null;

这是代码,希望你能理解我的问题

public class YourAsyncTask extends AsyncTask<String, String, String> {

        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... strings) {
            //Call getData function from here
            getData();
            return null;

        }

        @Override
        protected void onPostExecute(String lenghtOfFile) {
            // do stuff after posting data
        }
    }

【问题讨论】:

标签: android android-activity android-asynctask


【解决方案1】:

您的方法签名

doInBackground() 实现的方法签名如下:

protected String doInBackground(String...strings)

方法签名被分解为访问修饰符、返回类型、标识符和参数。您的实现doInBackground() 的返回类型是String

Android AsyncTask

Official documentation 状态:

异步任务由 3 种通用类型定义,称为 Params、Progress 和 Result

这些是在您扩展 AsyncTask 类时定义的,正如您已经完成的那样:

public class YourAsyncTask extends AsyncTask<String, String, String>

第三个参数是你的结果类型,你在这里指定为String。这种类型是你的doInBackground()实现必须返回的内容

这意味着您的doInBackGround() 实现必须返回String 类型的值。返回null 也是可以接受的(它是任何对象数据类型的有效值)。通过删除return null,您的方法不会返回任何产生您所看到的错误消息的值。

This 可能有助于理解 Java 中的方法结构。

【讨论】:

    【解决方案2】:

    在其签名中定义了return 值的所有函数都必须返回一些内容。这与 C/C++ 不同。

    在这里你声明你的函数返回一个String

    protected String doInBackground(String... strings) {

    如果你不想返回任何东西,那么你应该改变你的方法和类定义如下:

            public class YourAsyncTask extends AsyncTask<String, String, Void> { // <- Notice the change also here
            ...
    
            @Override
            protected void doInBackground(String... strings) {
                ...
            }
            ...
        }
    

    【讨论】:

    • 如果你想将 C 和 C++ 带入其中,我认为应该注意的是,在 c++ 中不从非 void 函数返回值是未定义的行为(main 的情况除外)
    • 另请注意,将Void 指定为Result 参数仍需要return statement
    【解决方案3】:

    在您的代码中:

    YourAsyncTask extends AsyncTask<String, String, String>
    

    你扩展了 AsyncTask 这意味着:

    异步任务使用的三种类型如下:

    1. Params : 执行时发送给任务的参数类型。

    2. Progress : 期间发布的进度单位的类型

      背景计算。

    3. Result :结果的类型 后台计算。

    现在您将所有类型声明为字符串,您必须在 'doInBackground(String...strings)' 中返回字符串或 null

    如果您不想退货,请使用:

     public class YourAsyncTask extends AsyncTask<Void, Void, Void> {
    
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }
    
            @Override
            protected Void doInBackground(Void... params) {
                return null;
            }
    
            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
            }
        }
    

    【讨论】:

    • 使用Void作为返回值仍然需要return null;。实际上,返回任何其他内容都是错误的。但你是对的,如果不需要返回值,这就是应该使用的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-10
    • 1970-01-01
    • 2021-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多