gpt4 book ai didi

java - Object[] 不能在 AsyncTask 中强制转换为 Void[]

转载 作者:IT老高 更新时间:2023-10-28 21:07:31 25 4
gpt4 key购买 nike

我在扩展的 asynctask 中遇到了这个错误,但我确定 Object[] 是一个 Void[]。

这是我的自定义 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);
}

这是产生问题的子类:

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

最后,这是错误:

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$2.call(AsyncTask.java:288)

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

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

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

RepeatableAsyncTask.java:43:

result = repeatInBackground(inputs);

编辑 2:

我这样调用执行:

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

最佳答案

找到解决方案:

问题是这样的:

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

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

解决方案:

  1. 使用 ListPalinasAsynkTask 代替通用 AsyncTask
  2. 更好的方法:创建一个新类 VoidRepeatableAsyncTask 并让其他 Void AsyncTask 扩展该类。

像这样:

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

然后你可以很容易地使用这样的东西来调用执行:

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

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

关于java - Object[] 不能在 AsyncTask 中强制转换为 Void[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20455644/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com