Android Studio 2.1.2
I am having this warning and I can't seem to find why. I am not using any raw types.
Unchecked call to attachView(DownloadViewContract) as a member of raw type
I have the following interface
public interface DownloadPresenterContract {
interface Operations<DownloadViewContract> {
void attachView(DownloadViewContract view);
void detachView();
void getData();
}
}
And the following implementation
public class DownloadPresenterImp implements
DownloadPresenterContract.Operations<DownloadViewContract> {
private DownloadViewContract mView;
private DownloadPresenterImp() {
}
public static DownloadPresenterImp getNewInstance() {
return new DownloadPresenterImp();
}
/* Operations */
@Override
public void attachView(DownloadViewContract view) {
mView = view;
}
}
This is the interface for my view
public interface DownloadViewContract {
void onSuccessDownload();
void onFailureDownload(String errMsg);
}
And in my fragment which is my view
public class DownloadView extends Fragment implements DownloadViewContract {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
mDownloadPresenterContract = DownloadPresenterImp.getNewInstance();
/* UNCHECKED MEMBER TO RAW TYPE */
mDownloadPresenterContract.attachView(DownloadView.this);
}
.....
}
I don't understand why I am getting the warning as I am explicitly naming the DownloadViewContract
as the type in my presenter interface. And as my view implements the DownloadViewContract
interface I don't think there should be any problem.
Many thanks for any suggestions,
The warning tells you that the compiler has encountered a condition that it can't guarantee the sense of. You should avoid having this kind of thing.
A "raw" type in Java is a class which is non-generic and deals with "raw" Objects, rather than type-safe generic type parameters. For example, before Java generics was available, you would use a collection class like this: LinkedList list = new LinkedList(); list.
Why this behavior ?
The raw type is the generic type without any arguments. For example, ArrayList<String>
and ArrayList<MyObject>
are generic types, while ArrayList
is a raw type. You can mix uses of raw types and generic types, but the compiler will give a warning if it cannot tell whether a statement or expression is type safe,
ArrayList myList;
myList = new ArrayList();
myList.add("abc"); // “unchecked call to add(E) as a member of the raw type java.util.ArrayList
Integer s = (Integer) myList.get(0);
Consider the above code, where myList’s type is a raw type. A warning is given on the call to myList.add because it cannot be determined what type of elements are allowed in myList. The warning is: “unchecked call to add(E) as a member of the raw type java.util.ArrayList”. There is no warning for the fifth line, but it’s obvious that a class cast exception will be thrown at runtime because myList.get(0) is not an Integer.
Refer this for more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With