I’m trying to make the following code work with no success
protected BackgroundTask<?> backgroundTask = null;
...
protected <T> void confirmBackgroundAction(final BackgroundTask<T> task, final T arg) {
backgroundTask = task;
backgroundTask.attach(AbstractWorkerActivity.this);
backgroundTask.execute(arg);
}
Capture class looks like this:
public abstract class BackgroundTask<T> extends AsyncTask<T, Void, Long> {
...
}
With this code I’m having the following compilation error:
The method execute(capture#26-of ?...) in the type AsyncTask<capture#26-of ?,Void,Long> is not applicable for the arguments (T)
If I replace
backgroundTask.execute(arg);
with
((BackgroundTask<T>)backgroundTask).execute(arg);
I’m not having any compilation error anymore, but I have a ClassCastException at Runtime.
Is there a way to pass an instance of the BackgroundTask + the argument to my function ?
Edit: I forget the code from where I call the confirmBackgroundAction() method…
confirmBackgroundAction(new SpecificBackgroundTask(), (Void) null);
Where SpecificBackgroundTask looks like
public class SpecificBackgroundTask extends BackgroundTask<Void> {
...
}
At compile time, the
TinBackgroundTask<T>is unknown and can at most (theoretically) store 1 type T of multiple possible types with which you can callconfirmBackgroundAction().The compiler has no way to solve this consistently. Changing
Tinto aninterfaceyou define would fix the ambiguity.