I have this class
public final class AsyncTaskManager<T extends AsyncTask<?, ?, ?> & IProgressTrackerTask
& ICancelTask>
....
T mAsyncTask;
....
public void setupTask(T asyncTask) {
mAsyncTask = asyncTask;
mAsyncTask.execute();
}
.....
But on the line
mAsyncTask.execute();
Eclipse says:
Type safety: A generic array of capture#4-of ? is created for a varargs parameter
add @SuppressWarnings("unchecked")
I’ve added suppressWarning but I have ClassCastException during executing doInBackgound.
As said here all types must be checked before executing AsyncTask.
How can I solve my problem – make my class generic and disable this warning?
Thanks a lot!
EDITED:
Just to clarify how is used. I have abstact AsyncTask:
public abstract class ManagedAsyncTask<T, E, G> extends AsyncTask<T, E, G>
implements IProgressTrackerTask, ICancelTask {
and AsyncTask that’s doing real work:
public class GetTask extends ManagedAsyncTask<Void, Void, SearchResult> {
and in activity:
mAsyncTaskManager = new AsyncTaskManager<GetTask>(this, this);
mAsyncTaskManager.setupTask(new GetTask());
I’m not too sure about the generic part of it, but I think you’re getting that error because the
executemethod on an AsyncTask expects as an argument the parameters of thedoInBackgroundmethod. This will be an array of whatever type the first question mark ofAsyncTask<?, ?, ?>. If you’re not expecting any data in yourdoInBackground, you can pass(Void[])null(essentially an array of nothing) in order to satisfy the compiler.Here’s the documentation for the
execute()method of the AsyncTask. If you’re still having problems, other parts of this page can be very helpful for configuring and using your AsyncTask:http://developer.android.com/reference/android/os/AsyncTask.html#execute%28Params…%29
Edit: Just to clarify, the
Voidin(Void[])nullshould be of whatever type the parameter ofdoInBackgroundis. I’m assuming, since you aren’t passing anything into execute, that you’re using Void as that parameter to indicate that there’s no necessary data. You may have implemented it differently and need to cast null accordingly.