gpt4 book ai didi

java - 未经检查的对原始类型成员的调用

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:48:35 24 4
gpt4 key购买 nike

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

这是我的 View 界面

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

在我的 fragment 中,这是我的观点

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 命名为我的演示者界面中的类型。由于我的 View 实现了 DownloadViewContract 接口(interface),我认为应该不会有任何问题。

非常感谢您的任何建议,

最佳答案

为什么会出现这种行为?

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

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

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

引用this了解更多详情。

关于java - 未经检查的对原始类型成员的调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38036442/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com