Java 对原始类型成员的未经检查的调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38036442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
unchecked call to member of raw type
提问by ant2009
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.
我不明白为什么我会收到警告,因为我DownloadViewContract
在演示者界面中明确命名为类型。当我的视图实现DownloadViewContract
接口时,我认为应该不会有任何问题。
Many thanks for any suggestions,
非常感谢您的任何建议,
采纳答案by x0r
I believe you declare mDownloadPresenterContract without specifying the type, declare it like this instead:
我相信你在没有指定类型的情况下声明了 mDownloadPresenterContract,而是像这样声明它:
DownloadPresenterContract.Operations<DownloadViewContract> mDownloadPresenterContract = DownloadPresenterImp.getNewInstance();
mDownloadPresenterContract.attachView(DownloadView.this);
回答by prime
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<String>
andArrayList<MyObject>
是泛型类型,而 whileArrayList
是原始类型。您可以混合使用原始类型和泛型类型,但如果编译器无法判断语句或表达式是否类型安全,则会发出警告,
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.
考虑上面的代码,其中 myList 的类型是原始类型。调用 myList.add 时会发出警告,因为无法确定 myList 中允许使用什么类型的元素。警告是:“未经检查调用 add(E) 作为原始类型 java.util.ArrayList 的成员”。第五行没有警告,但很明显在运行时会抛出类转换异常,因为 myList.get(0) 不是一个整数。
Refer thisfor more details.
有关更多详细信息,请参阅此内容。