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

◇◆丶佛笑我妖孽 提交于 2019-11-30 04:24:53

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.

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();

Try using:

result = repeatInBackground((Void) inputs);

instead of

result = repeatInBackground(inputs);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!