java – Unchecked Call to 'execute(Params…)' as a member of raw type 'android.os.AsyncTask'-ThrowExceptions

Exception or error:

I’m following the google android tutorial on Udacity but in the specified code, I’m getting the following warning:

Unchecked Call to ‘execute(Params…)’ as a member of raw type ‘android.os.AsyncTask’

on this code:

DoSomethingTask myTask = new DoSomethingTask();
myTask.execute(); // Warning here

DoSomethingTask:

public class DoSomethingTask extends AsyncTask {

    protected Object doInBackground(Object[] params) {
        ...
    }
}

Can anyone explain this warning and how to fix it? It seems it should work according to the instructions…

How to solve:

The warning is caused by the params for the task. Try to use:

extends AsyncTask<Void, Void, Void>{
    protected Object doInBackground() {
    }
}

or use:

extends AsyncTask<Object, Void, Void>{
    protected Object doInBackground(Object[] params) {
    }
}
myTask.execute(anyObject);

Explanation:

This document explains the meaning of the three types for AsyncTask.

  1. Params, the type of the parameters sent to the task upon execution.

  2. Progress, the type of the progress units published during the background computation.

  3. Result, the type of the result of the background computation.

###

change

AsyncTask asyncTask = new AsyncTask<Object, Void, String>() {

        @Override
        protected String doInBackground(Object... params) {
            return "";
        }
    };

    asyncTask.execute();

to

AsyncTask<Object, Void, String> asyncTask = new AsyncTask<Object, Void, String>() {

        @Override
        protected String doInBackground(Object... params) {
            return "";
        }
    };

    asyncTask.execute();

AsyncTask reference need the same as AsyncTask Class ,and IDE will not warning 。

Leave a Reply

Your email address will not be published. Required fields are marked *