0

我得到一个带有以下代码的 NotSerializableException ,但我不知道为什么:

private class Entry implements Serializable {
    public int mProgress, mReps;
    public int mDays;
    public String[] mEntry;

    public Entry() {
        mEntry = new String[2];
        mProgress = mReps = 0;
        mDays = 0;
    }
}

private HashMap<String,Entry> mEntries;

FileOutputStream fos = mApp.openFileOutput("FOO", Context.MODE_PRIVATE);
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(mEntries);
os.close();

我发现了上述 4 行之一中发生的异常:

保存异常 java.io.NotSerializableException: com.company.app.classname

4

1 回答 1

4

我怀疑这Entry是一个内部类,因此将引用可能不可序列化的“外部”类。

尝试以下操作:

private static class Entry implements Serializable {
...
}

注意static关键字。

编辑:

正如@Henrik 所指出的,查看确切的异常消息和堆栈跟踪通常会提供重要的线索。在这种情况下,异常显示了不可序列化的类的com.company.app.classname名称Entry

于 2013-03-06T14:13:46.123 回答