简体   繁体   中英

unchecked call to member of raw type

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,

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.

I believe you declare mDownloadPresenterContract without specifying the type, declare it like this instead:

DownloadPresenterContract.Operations<DownloadViewContract> mDownloadPresenterContract = DownloadPresenterImp.getNewInstance();

mDownloadPresenterContract.attachView(DownloadView.this);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM