gpt4 book ai didi

android - 为什么我会收到警告 Serialize ArrayList

转载 作者:行者123 更新时间:2023-11-29 02:11:01 25 4
gpt4 key购买 nike

我在读取可序列化对象的 ArrayList 时收到奇怪的警告。这是代码:

public void loadBoard() {
FileInputStream fis = null;
ObjectInputStream is;
try {
fis = this.openFileInput(saveFile);
is = new ObjectInputStream(fis);
// Build up sample vision board
if (mVisionBoard == null) {
mVisionBoard = new ArrayList<VisionObject>();
} else {
mVisionBoard.clear();
}
ArrayList<VisionObject> readObject = (ArrayList<VisionObject>) is.readObject();
mVisionBoard = readObject;
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.e(TAG, "loadBoard failed: "+e);
} catch (StreamCorruptedException e) {
e.printStackTrace();
Log.e(TAG, "loadBoard failed: "+e);
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "loadBoard failed: "+e);
} catch (ClassNotFoundException e) {
e.printStackTrace();
Log.e(TAG, "loadBoard failed: "+e);
}
}

我收到的警告是(在 readObject 行):

"Type safety: unchecked cast from Object to ArrayList"

我读过的几个示例表明这是读取可序列化对象的 ArrayList 的正确代码。我编写的用于编写 arraylist 的代码没有给我任何警告。我在这里做错了什么吗?

最佳答案

有点晚了,但它会帮助某人...

警告的原因是因为方法返回 readObject ...

见:

  public final Object readObject()

它实际上返回一个对象

如果您只是错误地读取并反序列化了一个让我们说 String 对象的 Ant 尝试将其转换为数组列表,那么您将获得运行时执行(原因一定很明显)

为了避免可预测的失败,您可以在转换之前检查返回对象的类型...

这就是你收到警告的原因:

"Type safety: unchecked cast from Object to ArrayList<VisionObject>"

关于android - 为什么我会收到警告 Serialize ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7390151/

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