简体   繁体   English

java.io.NotSerializableException Libgdx

[英]java.io.NotSerializableException Libgdx

I'm trying to implement a save feature for my LibGdx game. 我正在尝试为我的LibGdx游戏实现保存功能。 It's just for learning purposes for me so I'm trying to recreate the Fallout Shelter game on mobile phones, but just as a desktop version. 它仅供我学习之用,因此我尝试在手机上重新创建“辐射防护”游戏,但仅作为台式机版本。 When I attempt to save or load I get this error: 当我尝试保存或加载时,出现此错误:

java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: com.badlogic.gdx.utils.Array

And here is the load method, which is called when the game launches and always results in error 这是load方法,在游戏启动时会调用它,并且总是导致错误

private void Load() {
    try {
        // Open file to read from, named SavedObj.sav.
        FileInputStream saveFile = new FileInputStream("SaveGame.sav");

        // Create an ObjectInputStream to get objects from save file.
        ObjectInputStream save = new ObjectInputStream(saveFile);

        energy = (Integer) save.readObject();
        System.out.println(energy);
        food = (Integer) save.readObject();
        System.out.println(food);
        water = (Integer) save.readObject();
        System.out.println(water);
        maxEnergy = (Integer) save.readObject();
        System.out.println(maxEnergy);
        maxFood = (Integer) save.readObject();
        System.out.println(maxFood);
        maxWater = (Integer) save.readObject();
        System.out.println(maxWater);
        caps = (Integer) save.readObject();
        System.out.println(caps);
        maxDwellers = (Integer) save.readObject();
        System.out.println(maxDwellers);
        dwellers = (Array<Dweller>) save.readObject();
        //Close the save file
        save.close(); 
    } catch (Exception exc) {
        exc.printStackTrace(); // If there was an error, print the info.
    }
}

It always finds a problem with the line when I attempt to load the list of Dwellers (Saving has the same errors and finds fault in the same line) 当我尝试加载居民列表时,它总是会在线路上发现问题(保存具有相同的错误并在同一行中发现故障)

dwellers = (Array<Dweller>) save.readObject();

Where it then gives the error mentioned above. 然后出现上面提到的错误。 The class this is in is using java.io.Serializable and has it implemented, as other questions like this have suggested, but I still get the same error whether I have it implemented or not. 该类所在的类正在使用java.io.Serializable并已实现,如其他类似问题所建议的那样,但是无论是否实现,我仍然会遇到相同的错误。

The exception is saying that you are trying to serialize an instance of com.badlogic.gdx.utils.Array ... and you can't. 唯一的例外是,您正在尝试序列化com.badlogic.gdx.utils.Array的实例, com.badlogic.gdx.utils.Array不能。

According to the javadoc , the Array class that you are attempting to serialize does not implement Serializable . 根据javadoc ,您要序列化的Array类未实现Serializable

You haven't provided enough information for a proper diagnosis / fix, but I suspect that Dweller directly or indirectly uses the Array class, and that is the root cause. 您没有提供足够的信息来进行正确的诊断/修复,但我怀疑Dweller直接或间接使用Array类,这是根本原因。 Possible solutions might be to mark the Array field transient, and / or implement custom serialization methods ( readObject , writeObject and so on). 可能的解决方案可能是标记Array字段为瞬态,和/或实现自定义序列化方法( readObjectwriteObject等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM