public static void save()
{
BufferedWriter out = null;
try
{
out = new BufferedWriter(new OutputStreamWriter(Gdx.files.external(file).write(false)));
out.write(Double.toString(FinanceSystem.currentPlayerCash));
out.write("\n");
out.write(Integer.toString(DateSystem.day));
out.write("\n");
out.write(Integer.toString(DateSystem.month));
out.write("\n");
out.write(Integer.toString(DateSystem.year));
out.write("\n");
for(int i = 0; i <= InventorySystem.drugsOwned.size(); i++)
out.write(Integer.toString(InventorySystem.drugsOwned.get(i))+"\n");
for(int i = 0; i <= AttributeSystem.attributeNames.length; i++)
out.write(Integer.toString(AttributeSystem.attributeValues.get(i)) + "\n");
}
catch (Throwable e) {}
finally
{
try
{
if (out != null)
out.close();
}
catch (IOException e) {}
}
我的问题是,在库存系统的for循环之后,没有其他东西被写入文件。所以在这个例子中,AttributeSystem.attributeValues没有写入。我也尝试过在这个循环之后编写其他东西,包括非循环的东西,他们也没有写。 Whagwaan?
答案 0 :(得分:7)
这是问题所在:
for(int i = 0; i <= InventorySystem.drugsOwned.size(); i++)
out.write(Integer.toString(InventorySystem.drugsOwned.get(i))+"\n");
<=
应该是<
。因此,如果集合中有5个项目,则需要元素5,即第6个元素,因为索引是从0开始的。这会引发异常。
然后被这掩盖:
catch (Throwable e)
{
}
在诊断方面,你完全在这里完成自己的工作:
Exception
很糟糕;捕获Throwable
更糟糕。)(另外,代码建议你要么过度使用静态变量,要么需要整理你的命名约定。但这是另一回事。)