Search code examples
androidcastingwarningsunchecked

Arraylist : uses unchecked or unsafe operations


I have two warning in my app that prevent me for an "uses unchecked or unsafe operations". I tried to understand another posts but I do not understand at all what happens so I'm still at the starting point...

One in my Adapter class (the warning in on "(ArrayList) results.values") :

arrayList = (ArrayList<Sound>) results.values;

enter image description here

And another in my Activity where I am using the arraylist (the warning is on "Collections.sort") :

        Collections.sort(names);

enter image description here

Can you explain me what does it means and how to solve it please ?

Thank's for your help.


Solution

  • The type of results.values is apparently Object, so casting it to an ArrayList<Sound> is dangerous since its run-time type could be incompatible with that type. If result.values is always an ArrayList<Sound> and you have the ability to change its type, then simply change it to ArrayList<Sound>. If you absolutely don't have the ability to change the type and you are absolutely certain that the cast will be valid at run-time, then you can add @SuppressWarnings("unchecked") above that line to suppress the warning.

    I don't know what the type of names is in your excerpt, but it's likely that its compareTo() method has a similarly unsafe cast, due to underspecifying the parameter type. Look for a problem similar to this question.