尝试将对象写入libgdx

时间:2015-11-20 03:50:13

标签: java android file io libgdx

我一直收到这个错误。我试图将一个对象保存到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;
}
}

感谢您的时间!任何帮助是极大的赞赏。我一直被困在这上面。我只想保存一个高分。

1 个答案:

答案 0 :(得分:0)