无法序列化HashMap

时间:2015-05-20 16:11:40

标签: java android serialization

我试图序列化 HashMap,但它正在投掷NotSerializableException

我将HashMap声明为:

HashMap<String, Profile> profiles = new HashMap<String, Profile>();

这是我的代码:

ObjectOutputStream objectOutput = new ObjectOutputStream(openFileOutput(name, Context.MODE_PRIVATE));
objectOutput.writeObject(profiles);
objectOutput.flush()

Profile.java

import java.io.Serializable;

public class Profile implements Serializable {
    String name;
    String server;
    int port;
    boolean useSSL;
    boolean autoConnect;
    String nick;
    String alterNick;
    String realname;
    String username;
    String serverPass;
    String nickservPass;
    String[] autojoinChannels;
    String[] connectCommands;
}

元素初始化如下:

Profile profile = new Profile() {{
    name = "SOMETHING";
    useSSL = false;
    // etc. etc.
}};

logcat的:

java.io.NotSerializableException: tk.microdroid.microirc.MainActivity
        at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1364)
        at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
        at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:979)
        at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:368)
        at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1074)
        at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
        at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
        at java.util.HashMap.writeObject(HashMap.java:1013)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1053)
        at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
        at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
        at tk.microdroid.microirc.MainActivity.saveObject(MainActivity.java:168)
        at tk.microdroid.microirc.MainActivity.saveDefaultProfiles(MainActivity.java:201)
        at tk.microdroid.microirc.MainActivity.checkFirstStartup(MainActivity.java:70)
        at tk.microdroid.microirc.MainActivity.onCreate(MainActivity.java:43)
        at android.app.Activity.performCreate(Activity.java:4465)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
        at android.app.ActivityThread.access$600(ActivityThread.java:127)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4448)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
        at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
        at dalvik.system.NativeStart.main(Native Method)

修改

  • Profile类在单独的文件中定义,而不是内部类

  • 以下是saveObject()和loadObject()方法

    public Object loadObject(String name) {
        try {
            ObjectInputStream objectInput = new ObjectInputStream(openFileInput(name));
            Object obj = objectInput.readObject();
            objectInput.close();
            Log.v("loadObject()", "Loading " + name + ".. | Type: " + obj.getClass().getName());
            return obj;
        } catch (Exception e) { Log.e("loadObject()", e.toString()); }
        return null;
    }
    
    public void saveObject(Object obj, String name) {
        try {
            Log.v("saveObject()", "Saving " + name + ".. | Type: " + obj.getClass().getName());
            ObjectOutputStream objectOutput = new ObjectOutputStream(openFileOutput(name, MODE_PRIVATE));
            objectOutput.writeObject(obj);
            objectOutput.flush();
            objectOutput.close();
        } catch (Exception e) { Log.e("saveObject()","Error", e); }
    }
    

2 个答案:

答案 0 :(得分:12)

最后,我能够解决我的问题

我将对象变量设置为:

Profile profile = new Profile() {{
    name = "SOMETHING";
    useSSL = false;
    // etc. etc.
}};

所以我只是尝试设置变量而不使用双括号,例如:

profile.name = " SOMETHING";
profile.useSSL = false;
// etc. etc.

我甚至不确定为什么会导致这个问题,但最后我已经解决了这个问题。

答案 1 :(得分:3)

如果Profile是MainActivity的嵌套类,请将其声明为static。 (除此之外,我没有看到序列化框架试图序列化MainActivity的原因。)如果不是这样,请从MainActivity发布更多代码,包括saveObject,我们将更新答案。

非静态嵌套类具有对其容器类的隐式引用。因此序列化框架将尝试序列化您的MainActivity。