Java 未选中对原始类型成员的调用
我有这个警告,我似乎找不到原因。我没有使用任何原始类型Java 未选中对原始类型成员的调用,java,android,Java,Android,我有这个警告,我似乎找不到原因。我没有使用任何原始类型 Android Studio 2.1.2 我有以下接口 Unchecked call to attachView(DownloadViewContract) as a member of raw type 在我的片段中,那是我的观点 public interface DownloadViewContract { void onSuccessDownload(); void onFailureDownload(String
Android Studio 2.1.2
我有以下接口
Unchecked call to attachView(DownloadViewContract) as a member of raw type
在我的片段中,那是我的观点
public interface DownloadViewContract {
void onSuccessDownload();
void onFailureDownload(String errMsg);
}
我不明白为什么会收到警告,因为我在演示者界面中将DownloadViewContract
明确命名为类型。由于我的视图实现了DownloadViewContract
接口,所以我认为不应该有任何问题
非常感谢您的建议,我相信您在声明mDownloadPresenterContract时没有指定类型,而是这样声明:
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);
}
.....
}
DownloadPresenterContract.Operations mDownloadPresenterContract=DownloadPresenterImp.getNewInstance();
mDownloadPresenterContract.attachView(DownloadView.this);
为什么会有这种行为
原始类型是没有任何参数的泛型类型。例如,ArrayList
和ArrayList
是泛型类型,而ArrayList
是原始类型。您可以混合使用原始类型和泛型类型,但如果编译器无法判断语句或表达式是否是类型安全的,则会发出警告
DownloadPresenterContract.Operations<DownloadViewContract> mDownloadPresenterContract = DownloadPresenterImp.getNewInstance();
mDownloadPresenterContract.attachView(DownloadView.this);
考虑上面的代码,其中myList的类型是原始类型。调用myList.add时会发出警告,因为无法确定myList中允许的元素类型。警告是:“未选中将(E)添加为原始类型java.util.ArrayList的成员的调用”。第五行没有警告,但很明显在运行时会引发类强制转换异常,因为myList.get(0)不是整数
有关更多详细信息,请参阅。能否显示
mDownloadPresenterContract
的声明?我的问题中已经有了DownloadPresenterContract。公共接口下载PresenterContract{interface Operations{void attachView(DownloadViewContract视图);void detachView();void getData();}}}谢谢,我不知道我怎么会错过这个。这个答案解释得很好,应该是可以接受的答案。
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);
}
.....
}
DownloadPresenterContract.Operations<DownloadViewContract> mDownloadPresenterContract = DownloadPresenterImp.getNewInstance();
mDownloadPresenterContract.attachView(DownloadView.this);
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);