Java Object[] 不能在 AsyncTask 中转换为 Void[]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20455644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:04:09  来源:igfitidea点击:

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

javaandroidvoid

提问by Luca Vitucci

I'm getting this error inside an extended asynctask, but i'm really sure that Object[] IS a Void[].

我在扩展的异步任务中收到此错误,但我非常确定 Object[] 是一个 Void[]。

This is my custom AsyncTask:

这是我的自定义 AsyncTask:

public abstract class RepeatableAsyncTask<A, B, C> extends AsyncTask<A, B, C> {
private static final String TAG = "RepeatableAsyncTask";
public static final int DEFAULT_MAX_RETRY = 5;

private int mMaxRetries = DEFAULT_MAX_RETRY;
private Exception mException = null;

/**
 * Default constructor
 */
public RepeatableAsyncTask() {
    super();
}

/**
 * Constructs an AsyncTask that will repeate itself for max Retries
 * @param retries Max Retries.
 */
public RepeatableAsyncTask(int retries) {
    super();
    mMaxRetries = retries;
}

/**
 * Will be repeated for max retries while the result is null or an exception is thrown.
 * @param inputs Same as AsyncTask's
 * @return Same as AsyncTask's
 */
protected abstract C repeatInBackground(A...inputs);

@Override
protected final C doInBackground(A...inputs) {
    int tries = 0;
    C result = null;

    /* This is the main loop, repeatInBackground will be repeated until result will not be null */
    while(tries++ < mMaxRetries && result == null) {
        try {
            result = repeatInBackground(inputs);
        } catch (Exception exception) {
            /* You might want to log the exception everytime, do it here. */
            mException = exception;
        }
    }
    return result;
}

/**
 * Like onPostExecute but will return an eventual Exception
 * @param c Result same as AsyncTask
 * @param exception Exception thrown in the loop, even if the result is not null.
 */
protected abstract void onPostExecute(C c, Exception exception);

@Override
protected final void onPostExecute(C c) {
    super.onPostExecute(c);
    onPostExecute(c, mException);
}

And this is the SubClass that gives the problem:

这是给出问题的子类:

public class ListPalinasAsynkTask extends RepeatableAsyncTask<Void, Void, List<Palina>>{
    private static final String TAG = "ListPalinasAsynkTask";
    private static final boolean D = SystemConstants.ACTIVE_DEBUG;

    private ApiRequest.ListPalinasCallback mCallback;

    public ListPalinasAsynkTask(ApiRequest.ListPalinasCallback callback) {
        if(D) Log.d(TAG, "Called: ListPalinasAsynkTask([callback])");
        mCallback = callback;
    }

    @Override
    protected List<Palina> repeatInBackground(Void... voids) {
        /* Here i send request to get palinas */
        return Api.getPalinasNearMe();
    }

    @Override
    protected void onPostExecute(List<Palina> palinas, Exception exception) {
        if(exception != null)
            Logging.e(TAG, "Received exception in Asynk Task", exception);
        if(palinas != null)
            mCallback.result(palinas);
        else
            mCallback.result(new ArrayList<Palina>());
    }
}

Finally, this is the error:

最后,这是错误:

E/ListPalinasAsynkTask﹕ Received exception in Asynk Task
    java.lang.ClassCastException: java.lang.Object[] cannot be cast to java.lang.Void[]
            at ListPalinasAsynkTask.repeatInBackground(ListPalinasAsynkTask.java:19)
            at RepeatableAsyncTask.doInBackground(RepeatableAsyncTask.java:43)
            at android.os.AsyncTask.call(AsyncTask.java:288)

I can't explain this exception because i'm giving Void as a parameter! That should not be an Object. Do you have solutions?

我无法解释这个异常,因为我给 Void 作为参数!那不应该是一个对象。你有解决方案吗?

Edit: ListPalinasAsyncTask.java:19 refers to:

编辑:ListPalinasAsyncTask.java:19 指的是:

public class ListPalinasAsynkTask extends RepeatableAsyncTask<Void, Void, List<Palina>> {

RepeatableAsyncTask.java:43:

RepeatableAsyncTask.java:43:

result = repeatInBackground(inputs);

EDIT 2:

编辑2:

I'm calling execute this way:

我通过这种方式调用执行:

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

采纳答案by Luca Vitucci

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.

我使用通用 AsyncTask 来调用 execute,该类将 Void 作为参数传递,并且永远不会在 ListPalinasAsynkTask 上调用 .execute(),而是调用 ListPalinasAsynkTask.execute(Void)。这给出了错误。

Solutions:

解决方案:

  1. Use ListPalinasAsynkTask instead of generic AsyncTask
  2. Better one: Create a new class VoidRepeatableAsyncTask and make other Void AsyncTasks extend that one.
  1. 使用 ListPalinasAsyncTask 而不是通用的 AsyncTask
  2. 更好的方法:创建一个新类 VoidRepeatableAsyncTask 并使其他 Void AsyncTask 扩展该类。

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.

这将调用 AsyncTask 的无参数执行方法。

回答by Raghav Sood

Try using:

尝试使用:

result = repeatInBackground((Void) inputs);

instead of

代替

result = repeatInBackground(inputs);

回答by Diolor

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

我解决它的另一种方法是传入Object参数,即使您不使用参数。

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.

如果您想将不同类型的 AsyncTasks 传递给方法,并且当然您不关心参数,则上述内容适用(在我的情况下)。

回答by marco

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

回答by Amit

If this problem is occurring then you might have created a Derived class object and added its the base class variable (AsyncTask). So this error is occurring.
AsyncTask mAsyncTask = new ListPalinasAsynkTask(callback);

如果发生此问题,那么您可能已经创建了派生类对象并添加了其基类变量 (AsyncTask)。所以这个错误正在发生。
AsyncTask mAsyncTask = new ListPalinasAsynkTask(callback);

Lint also gives a warning when you do like this "Unchecked call to execute. Params ...."

当您这样做时,Lint 也会发出警告“未经检查的执行调用。参数......”

The solution is ListPalinasAsynkTask mAsyncTask = new ListPalinasAsynkTask(callback); Then call execute on it. It will work fine

解决方法是 ListPalinasAsynkTask mAsyncTask = new ListPalinasAsynkTask(callback); 然后调用执行它。它会正常工作