我发现了一个扩展的AsyncTask这里面的错误,但我真的相信Object []对象是一个无[]。
这是我的自定义AsyncTask的:
公共抽象类RepeatableAsyncTask< A,B,C>扩展AsyncTask的< A,B,C> {
私有静态最后字符串变量=RepeatableAsyncTask;
公共静态最终诠释DEFAULT_MAX_RETRY = 5;
私人诠释mMaxRetries = DEFAULT_MAX_RETRY;
私人异常mException = NULL;
/ **
*默认构造函数
* /
公共RepeatableAsyncTask(){
超();
}
/ **
*构造一个AsyncTask的,将repeate本身最大重试次数
*参数重试最大重试。
* /
公共RepeatableAsyncTask(INT重试){
超();
mMaxRetries =重试;
}
/ **
*将重复最大重试次数,而结果是null或抛出异常。
* @参数输入相同的AsyncTask的
* @返回相同的AsyncTask的
* /
受保护的抽象ÇrepeatInBackground(A ...输入);
@覆盖
保护最后的C doInBackground(A ...输入){
INT试图= 0;
C测试结果= NULL;
/ *这是在主循环,repeatInBackground将被重复直到结果将不能为空* /
而(尝试++< mMaxRetries和放大器;&安培;结果== NULL){
尝试 {
结果= repeatInBackground(输入);
}赶上(例外的例外){
/ *你可能想记录异常每次,它在这里做。 * /
mException =例外;
}
}
返回结果;
}
/ **
*像onPostExecute但会返回一个最终的异常
*参数C测试结果一样的AsyncTask
*参数异常抛出异常的循环,即使结果不为空。
* /
保护抽象无效onPostExecute(C C,异常除外);
@覆盖
受保护的最终无效onPostExecute(C C){
super.onPostExecute(C);
onPostExecute(C,mException);
}
这是子类,让这个问题:
公共类ListPalinasAsynkTask扩展RepeatableAsyncTask<虚空,虚空,名单,其中,Palina>> {
私有静态最后字符串变量=ListPalinasAsynkTask;
私有静态最终布尔D = SystemConstants.ACTIVE_DEBUG;
私人ApiRequest.ListPalinasCallback mCallback;
公共ListPalinasAsynkTask(ApiRequest.ListPalinasCallback回调){
如果(D)Log.d(TAG,叫:ListPalinasAsynkTask([回调]));
mCallback =回调;
}
@覆盖
受保护的名单,其中,Palina> repeatInBackground(虚空......空隙){
/ *在这里,我发送请求来获取palinas * /
返回Api.getPalinasNearMe();
}
@覆盖
保护无效onPostExecute(名单< Palina> palinas,例外的例外){
如果(例外!= NULL)
Logging.e(TAG,在Asynk任务接收例外,例外);
如果(palinas!= NULL)
mCallback.result(palinas);
其他
mCallback.result(新的ArrayList&其中; Palina>());
}
}
最后,这是错误:
E / ListPalinasAsynkTask:在Asynk任务接收到异常
java.lang.ClassCastException:的java.lang.Object []不能被转换为java.lang.Void的[]
在ListPalinasAsynkTask.repeatInBackground(ListPalinasAsynkTask.java:19)
在RepeatableAsyncTask.doInBackground(RepeatableAsyncTask.java:43)
在android.os.AsyncTask $ 2.call(AsyncTask.java:288)
我无法解释这种异常,因为我给太虚作为参数!这不应该是一个对象。 你有没有解决方案?
修改: ListPalinasAsyncTask.java:19指的是:
公共类ListPalinasAsynkTask扩展RepeatableAsyncTask<虚空,虚空,名单,其中,Palina>> {
RepeatableAsyncTask.java:43:
的结果= repeatInBackground(输入);
编辑2:
我打电话是执行这种方式:
的AsyncTask mAsyncTask =新ListPalinasAsynkTask(回调);
....
mAsyncTask.execute();
解决方案
解决方案发现:
这个问题是这样的:
的AsyncTask mAsyncTask =新ListPalinasAsynkTask(回调);
....
mAsyncTask.execute();
我使用的是通用的AsyncTask来调用执行,该类会通过太虚作为参数,并永远不会调用.execute()上ListPalinasAsynkTask,相反,它会调用ListPalinasAsynkTask.execute(无效)。这给了错误。
解决方案:
使用ListPalinasAsynkTask,而不是通用的AsyncTask 更好之一:创建一个新类VoidRepeatableAsyncTask并进行其他太虚AsyncTasks延长了一个这样的:
公共抽象类VoidRepeatableAsyncTask< T>扩展RepeatableAsyncTask<虚空,虚空,T> {
公共无效的execute(){
super.execute();
}
}
然后就可以方便地使用这样的调用执行:
VoidRepeatableAsyncTask mAsyncTask =新ListPalinasAsynkTask(回调);
....
mAsyncTask.execute();
这将调用的AsyncTask的无参数的execute方法。
I'm getting this error inside an extended asynctask, but i'm really sure that Object[] IS a Void[].
This is my custom 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$2.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?
Edit: ListPalinasAsyncTask.java:19 refers to:
public class ListPalinasAsynkTask extends RepeatableAsyncTask<Void, Void, List<Palina>> {
RepeatableAsyncTask.java:43:
result = repeatInBackground(inputs);
EDIT 2:
I'm calling execute this way:
AsyncTask 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:
Use ListPalinasAsynkTask instead of generic AsyncTask 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.