我一直收到这个错误。我试图将一个对象保存到libGDX中的文件。我首先创建文件,然后创建一个新的高分并保存。但它没有用,我尝试使得Score类实现可序列化并使得得分对象成为瞬态。两者都没有奏效。我尝试在libGDX上运行桌面版。
java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: com.bayanijulian.covertpony.save.Score
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1355)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)
at com.bayanijulian.covertpony.save.SaveHandler.load(SaveHandler.java:35)
有我的课程。
package com.bayanijulian.covertpony.save;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.bayanijulian.covertpony.TSIEngine;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class SaveHandler{
public static void save(){
FileHandle file = Gdx.files.local("score.txt");
try{
ObjectOutputStream scoreOutput = new ObjectOutputStream(file.write(false));
scoreOutput.writeObject(TSIEngine.score);
scoreOutput.close();
}
catch (Exception e){
//TODO fix crash
e.printStackTrace();
}
}
public static void load(){
FileHandle file = Gdx.files.local("score.txt");
try{
if(!file.exists()){
file.file().createNewFile();
TSIEngine.score = new Score("3:00","you");
save();
}
ObjectInputStream scoreInput = new ObjectInputStream(file.read());
TSIEngine.score = (Score) scoreInput.readObject();
scoreInput.close();
}
catch (Exception e){
//TODO fix crash
e.printStackTrace();
}
}
}
这是我尝试写入文件的Score对象。
package com.bayanijulian.covertpony.save;
import java.io.Serializable;
public class Score implements Serializable{
private String time;
private String name;
public Score(String time, String name) {
this.time = time;
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
感谢您的时间!任何帮助是极大的赞赏。我一直被困在这上面。我只想保存一个高分。
答案 0 :(得分:0)
尝试使用LibGDX Preferences类:
https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/Preferences.html
这是关于如何使用它的wiki教程: