【问题标题】:unchecked call to member of raw type对原始类型成员的未经检查的调用
【发布时间】:2016-06-26 08:05:54
【问题描述】:
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 接口,我认为应该没有任何问题。

非常感谢您的任何建议,

【问题讨论】:

  • 可以出示mDownloadPresenterContract的声明吗?
  • DownloadPresenterContract 在我的问题中已经存在。公共接口 DownloadPresenterContract { interface Operations { void attachView(DownloadViewContract view);无效分离视图();无效的getData(); } }

标签: java android


【解决方案1】:

为什么会有这种行为?

原始类型是没有任何参数的泛型类型。例如,ArrayList&lt;String&gt;ArrayList&lt;MyObject&gt;泛型类型,而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

【讨论】:

  • 这个有很好解释的答案应该是公认的答案。
【解决方案2】:

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

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

mDownloadPresenterContract.attachView(DownloadView.this);

【讨论】:

  • 谢谢,不知道我怎么错过了。
猜你喜欢
  • 1970-01-01
  • 2018-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-17
  • 1970-01-01
  • 1970-01-01
  • 2016-10-22
相关资源
最近更新 更多