0

好的,我会尽量说清楚。我希望在我得到答案之前它不会被标记。

我正在尝试序列化一个用于保存的类,它工作得很好。但是有些变量没有正确加载。这是代码:我还标出了代码工作和不工作的地方。

编辑:简化代码多一点。

类即时保存:(它的简单版本)

public class classToSave implements Serializable {


/**
 * 
 */
private static final long serialVersionUID = -5184436214950145051L;

public static List<classToSave> classtosavelist = new ArrayList<classToSave>();

public static classToSave one   = new OtherClass1("1", 1, 1, null, Item.1, 1); 
public static classToSave two   = new OtherClass1("2", 1, 2, one , Item.2, 1);  
public static classToSave three = new OtherClass2("3", 1, 4, two , Item.3, 2);

public boolean done = false;     < WONT LOAD/SAVE
public int lvl = 0;              < WONT LOAD/SAVE
transient protected String name; < OK
protected int Xpos;              < OK 
protected int Ypos;              < OK
public Skill parent;             < OK
protected Item item;             < OK
public int extra = 0;            < OK (This Works..)



public classToSave(String s, int x, int y, Skill parent, Item item, int cost) {
    this.name = s;
    this.Xpos = x;
    this.Ypos = y;
    if(parent != null) {
        this.Xpos += parent.Xpos;
        this.Ypos += parent.Ypos;
    }
    this.parent = parent;
    this.item = item;
    add(this, s);
}

private void add(classToSave classtosave, String name) {
    boolean flag = true;
    for(int i = 0; i < skills.size(); i++) {
        if(classtosavelist.get(i).getName().equalsIgnoreCase(name)) {
            flag = false;
        }
    }
    if(flag) {
        classtosavelist.add(classtosave);
    }
}

public int needPoints() {
    return 1 + extra;
}

public boolean Done(int points) {
    if(points >= needPoints()) {
        this.done = true;
    }
    return this.done;
}

public int getLevel() {
    return this.lvl;
}

public int MAXLevel() {
    return 1;
}

public void LevelUp() {
    if(this.lvl < MAXLevel()) {
        this.lvl++;
    }
}

public void Reset() {
    this.lvl = 0;
}

public Item getRenderItem() {
    return this.item;
}

public String getName() {
    return this.name;
}

public Skill getParent() {
    return this.parent;
}

public boolean isCompleteDone() {
    return (getLevel() == MAXLevel() && done);
}

这是保存和加载:

private ClassToSave classtosave;


private void save(String savename) {
    if (externalStorageWriteable) {
        try {
            File file = new File(ctxt.getExternalFilesDir(null), savename);
            FileOutputStream fos = new FileOutputStream(file, false);
            ObjectOutputStream os = new ObjectOutputStream(fos);

            os.writeObject(classtosave);

            os.flush();
            os.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

public void load(String savename) throws ClassNotFoundException, StreamCorruptedException, IOException {
    if (externalStorageAvailable) {
        File file = new File(ctxt.getExternalFilesDir(null), savename);
        FileInputStream fis = new FileInputStream(file);
        ObjectInputStream is = new ObjectInputStream(fis);


        ClassToSave classtosave = (ClassToSave) is.readObject();

             for (int i=0; i < classtosave.classtosavelist.size(); i++) {
                 classtosave.classtosavelist.get(i).Done(classtosave.classtosavelist.get(i).needPoints()); < WORKS
                 classtosave.classtosavelist.get(i).isCompleteDone(); < DOENST WORK
                 classtosave.classtosavelist.get(i).getLevel();  < DOESNT WORK


                 if (classtosave.classtosavelist.get(i).done == true){ <DOESNT WORK
                    classtosave.classtosavelist.get(i).done = true;
                    classtosave.classtosavelist.get(i).LevelUp();
                 }

             }

        }

        is.close();

}

对不起,如果括号不合适。这段代码被屠杀(重命名和修改)以使其更具可读性。

但基本上这段代码用于“技能树”,它应该做的是保存所述技能是否完整。

如果这需要修复,请告诉我。我还可以根据需要提供更多代码。

4

1 回答 1

0

您的构造函数错过了布尔值和 int。它应该是这样的:

public classToSave(boolean done, int lvl, String s, int x, int y, Skill parent, Item 
item, int cost) {
     this.done = done;
     this.lvl = lvl;
     ...
}
于 2013-08-20T15:08:50.367 回答