简体   繁体   English

Java如何处理来自Object的Array <MyItem>的“Unchecked cast”

[英]Java how to handle “Unchecked cast” for Array<MyItem> from Object

In my Android project I have made an abstract AsyncTask class in which I input the URL and if needed paging information so I don't need to keep writing the HTTP stuff etc. 在我的Android项目中,我创建了一个抽象的AsyncTask类,我在其中输入了URL,如果需要,还可以分页信息,因此我不需要继续编写HTTP内容等。

I've made an abstract method, onAsyncTaskResult(Object o) which must be implemented on use. 我已经创建了一个抽象方法onAsyncTaskResult(Object o),它必须在使用时实现。 However when casting it to the appropriate object (can be of different types) the IDE gives me a warning 但是,当将其转换为适当的对象(可以是不同类型)时,IDE会向我发出警告

"Unchecked cast for java.lang.Object to java.util.ArrayList<com.company.package.subpackage.MyItem>"

Here is my code snippet of the implemention of said function 这是我执行所述函数的代码片段

new SuperCoolAsyncTask() {
      @Override
      protected void onAsyncTaskResult(Object o) {
          if(o instanceof ArrayList) {
          //generates warning in the following line
          AppConstants.scoreStatistics = (ArrayList<MyItem>)o;
      }
   }
}.execute(get_url_score_statistics());

How am i supposed to cast this to an ArrayList<MyItem> without generating a warning? 我该如何将其转换为ArrayList<MyItem>而不生成警告?

Without the <MyItem> declaration it throws an "Unchecked assignment" 没有<MyItem>声明,它会抛出“未选中的任务”

You cannot do it without a warning. 没有警告就不能这样做。 You are casting an Object to some other class, and even if the Object is an instance of ArrayList you don't know it's generic type. 您正在将Object为其他类,即使ObjectArrayList的实例,您也不知道它是泛型类型。

Update: 更新:

if you wrote SuperCoolAsyncTask yourself, you could parametrize the class with generics: 如果您自己编写SuperCoolAsyncTask ,则可以使用泛型对类进行参数化:

public abstract class SuperCoolAsyncTask<ResultType> {

    protected abstract void onAsyncTaskResult(ResultType o);

}

And then, when you invoke your code: 然后,当您调用代码时:

new SuperCoolAsyncTask<ArrayList<MyItem>>() {
    @Override
    protected void onAsyncTaskResult(ArrayList<MyItem> o) {
            AppConstants.scoreStatistics = o;
    }
}.execute(get_url_score_statistics());

You can't avoid the warning, but you can hide it with a @SuppressWarnings annotation. 您无法避免警告,但可以使用@SuppressWarnings注释隐藏它。 Try this: 试试这个:

new SuperCoolAsyncTask() {
      @Override
      @SuppressWarnings("unchecked")
      protected void onAsyncTaskResult(Object o) {
          if(o instanceof ArrayList) {
          //generates warning in the following line
          AppConstants.scoreStatistics = (ArrayList<MyItem>)o;
      }
   }
}.execute(get_url_score_statistics());

The other option, if you're really worried about type safety, is to leave the generic unspecified (ie only cast to ArrayList, not ArrayList, and then cast each element as you remove it. 如果你真的担心类型安全,另一个选择是保留泛型未指定(即只转换为ArrayList,而不是ArrayList,然后在删除它时转换每个元素。

You can cast your object to ArrayList. 您可以将对象转换为ArrayList。 you can't cast it to your type 你不能把它投射到你的类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM