gpt4 book ai didi

java - 无法序列化 HashMap

转载 作者:太空狗 更新时间:2023-10-29 15:32:27 24 4
gpt4 key购买 nike

我正在尝试序列化一个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); }
    }

最佳答案

终于解决了我的问题

我将对象变量设置为:

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

所以我只是尝试在不使用双括号的情况下设置变量,例如:

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

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

关于java - 无法序列化 HashMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30355057/

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