0

我要将一个字符串数组列表保存到本地数据库的 1 列中。我在这样做时遇到了一些问题。谁能告诉我哪里出错了...

private String serializeArray(List<String> array) {
    try {
        ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bytesOut);
        oos.writeObject(array);
        oos.flush();
        oos.close();
        return Base64.encodeToString(bytesOut.toByteArray(), Base64.NO_WRAP);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

private ArrayList<String> deserializeArray(String string) {
    Log.d("USERDAO", string);
    try {
        ByteArrayInputStream bytesIn = new ByteArrayInputStream(Base64.decode(string, Base64.NO_WRAP));
        ObjectInputStream ois = new ObjectInputStream(bytesIn);
        return (ArrayList<String>) ois.readObject();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

在反序列化数组上返回 Arraylist 时出现空指针异常。serialiseArray 方法确实返回一个字符串,但我不确定它是否正确。

4

1 回答 1

0

当我在 Eclipse 中运行它时,我在这一行得到 java.lang.ClassCastException:

return (ArrayList<String>) ois.readObject();

readObject() 方法试图返回一个 Arrays$ArrayList (不管是什么),而你的演员导致它中断。如果您将反序列化的强制转换和返回类型更改为 List,您会发现一切正常。

于 2012-07-20T09:15:14.537 回答