gpt4 book ai didi

java - 无法序列化 Hashmap,java.io.NotSerializedException

转载 作者:行者123 更新时间:2023-12-01 20:54:52 25 4
gpt4 key购买 nike

public class Common implements Serializable{

private static HashMap<Integer,List<LevelList>> levelListMap = new HashMap<>();

public static Map<Integer, List<LevelList>> getLevelListMap(Context context) {

File file = new File(context.getDir("data", MODE_PRIVATE), "map");
ObjectInputStream inputStream = null;
try {
inputStream = new ObjectInputStream(new FileInputStream(file));
levelListMap = (HashMap<Integer, List<LevelList>>) inputStream.readObject();
} catch (Exception e) {
e.printStackTrace();
}
return levelListMap;
} ...
}

我无法序列化 hashmap。我不断收到 java.io.NotSerializableException为了 levelListMap = (HashMap<Integer, List<LevelList>>) inputStream.readObject();

public class LevelList implements Serializable{

public int id;
public String title;
public String imgurl;
public String songurl;
public String songtext;
boolean isFavourite;

public void release() {

}


public void setFavourite(boolean favourite) {
isFavourite = favourite;
}

public boolean isFavourite(){
return isFavourite;
}

}

最佳答案

HashMap是可序列化的,但键和值也必须是可序列化的。确保所有键和值都是可序列化的,并且它们的字段也是可序列化的(不包括 transient 和静态成员)。

编辑:

HashMap<Integer,List<LevelList>> ,是你的List实现可序列化?

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

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