簡體   English   中英

嘗試將對象寫入libgdx中的文件時出錯

[英]Error when trying to write an object to a file in libgdx

我不斷收到此錯誤。 我正在嘗試將一個對象保存到libGDX中的文件中。 我首先創建文件,然后創建新的高分並保存。 但這沒有用,我嘗試使Score類實現可序列化,並使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;
}
}

謝謝你的時間! 任何幫助是極大的贊賞。 我一直堅持下去。 我只想保存一個高分。

請嘗試使用LibGDX Preferences類:

https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/Preferences.html

這是有關如何使用它的維基教程:

https://github.com/libgdx/libgdx/wiki/首選項

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM