Object[] cannot be cast to Void[] in AsyncTask

The solution is much simpler as I see it. Just create the subclass object in this way:

AsyncTask<Void, Void, List<Palina> mAsyncTask = new ListPalinasAsynkTask(callback);
....
mAsyncTask.execute();

Solution found:

the problem was this:

AsyncTask mAsyncTask = new ListPalinasAsynkTask(callback);
....
mAsyncTask.execute();

I'm using generic AsyncTask to call execute, that class would pass Void as a parameter and will never call .execute() on ListPalinasAsynkTask, instead it will call ListPalinasAsynkTask.execute(Void). That gives the error.

Solutions:

  1. Use ListPalinasAsynkTask instead of generic AsyncTask
  2. Better one: Create a new class VoidRepeatableAsyncTask and make other Void AsyncTasks extend that one.

Like this:

public abstract class VoidRepeatableAsyncTask<T> extends RepeatableAsyncTask<Void, Void, T> {
    public void execute() {
        super.execute();
    }
}

Then you can easily use something like this to call execute:

VoidRepeatableAsyncTask mAsyncTask = new ListPalinasAsynkTask(callback);
....
mAsyncTask.execute();

This will call the no-parameters execute method of AsyncTask.


An alternative way with which I solved it is to pass Object in parameters, even if you don't use the parameters.

new AsyncTask<Object, Void, MergeAdapter>()

and the override:

@Override
protected ReturnClass doInBackground(Object... params) {
    //...
}

The above applies (in my case) if you want to pass different types of AsyncTasks to a method and of course you do not care about the parameters.

Tags:

Java

Android

Void