3

How do I get rid of the warning? The code I'm using:

private void tryNew(List<Something> somethingList){
    new AsyncTask<List<Something>,Void,Void>(){
        @Override
        protected Void doInBackground(List<Something> [] somethingList) {
            //some code
            return null;
        }
    }.execute(somethingList);
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
user3357751
  • 101
  • 1
  • 10
  • Wrap `List` in an object that isn't generic like `class Foo { public List list; }` and use `AsyncTask – zapl May 03 '16 at 13:49
  • Late addition, but worth noting that while adding `@SafeVarargs` to `doInBackground` is possible (and can help to eliminate an associated warning message - `Possible heap pollution from paramaterized vararg type`), it _does not_ remove the original "unchecked generics" warning, because you don't call `doInBackground` directly. You use `new AsyncTask(...).execute()`. – VerumCH Apr 13 '18 at 09:38

2 Answers2

4

I was with this same warning and could remove it just by using the object I use on the parameter of the execute method, without generics. Sorry my bad english, but I'll try to explain better.

Instead create my AsyncTask this way:

new AsyncTask<MyObject<GenericsInsideGenerics>, Void, Void>...

I've created this way:

new AsyncTask<MyObject, Void, Void>...

And it works fine!

Anderson Silva
  • 709
  • 1
  • 7
  • 31
1

If you want to remove the warning message then you have to use the designated varargs of AsyncTask.

private void tryNew(List<Something> somethingList){
    new AsyncTask<Something,Void,Void>(){
        @Override
        protected Void doInBackground(Something.. s) {
            //some code
            return null;
        }
    }.execute(somethingObject1, somethingObject2, somethingObject3);
}
Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107