对原始类型

时间:2016-06-26 08:05:55

标签: java android

Android Studio 2.1.2

我有这个警告,我似乎无法找到原因。我没有使用任何原始类型。

Unchecked call to attachView(DownloadViewContract) as a member of raw type

我有以下界面

public interface DownloadPresenterContract {
    interface Operations<DownloadViewContract> {
        void attachView(DownloadViewContract view);
        void detachView();
        void getData();
    }
}

以下实施

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;
    }
}

这是我视图的界面

public interface DownloadViewContract {
    void onSuccessDownload();
    void onFailureDownload(String errMsg);
}

在我的片段中,这是我的观点

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);
    }
    .....
}

我不明白为什么我收到警告,因为我明确地将DownloadViewContract命名为我的演示者界面中的类型。当我的视图实现DownloadViewContract接口时,我不认为应该有任何问题。

非常感谢任何建议,

2 个答案:

答案 0 :(得分:14)

为什么会出现这种情况?

原始类型是没有任何参数的泛型类型。例如,ArrayList<String>ArrayList<MyObject>泛型类型,而{{1是一个原始类型。您可以混合使用原始类型和泛型类型,但如果无法判断语句或表达式是否类型安全,编译器将发出警告,

ArrayList

考虑上面的代码,其中myList的类型是原始类型。调用myList.add时会发出警告,因为无法确定myList中允许的元素类型。警告是:“未选中调用添加(E)作为原始类型java.util.ArrayList”的成员。第五行没有警告,但显然会在运行时抛出类强制转换异常,因为myList.get(0)不是整数。

有关详细信息,请参阅this

答案 1 :(得分:5)

我相信你声明mDownloadPresenterContract而没有指定类型,而是声明它:

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

mDownloadPresenterContract.attachView(DownloadView.this);