我需要一种方法来保存对象的 ArrayList
。我在网站上浏览过类似的问题并且我(似乎 ;-) 已经实现了我发现的东西,但是我遇到了两个问题:
- 如果我将类定义为
Serializable
并放置构造函数,它会在启动时崩溃 - 否则不保存数组
你能帮忙吗?我正在为一个志愿者的项目开发代码,但我被困住了......
非常感谢您。
我的应用程序具有以下定义的类:Globals
(文件 Globals.java)
public class Globals extends Application implements Serializable {
private int position=-1;
private ArrayList<RaccoltaPunti> raccoltePuntiList = new ArrayList<RaccoltaPunti>();
public static final long serialVersionUID = 1L;
/** constructor - seem required by Serializable, but creating it crashes app */
public Globals(int position, ArrayList<RaccoltaPunti> raccoltePuntiList) {
this.position = position;
this.raccoltePuntiList = raccoltePuntiList;
}
// {getters and setters…}
public void saveData(){
String filename = getResources().getString(R.string.GLB_filename);
String fileWithPath = this.getFilesDir().getPath().toString()+"/"+filename;
Toast.makeText(this, "Salvataggio testo..."+ fileWithPath, Toast.LENGTH_LONG).show();
try {
FileOutputStream fos = new FileOutputStream(fileWithPath);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(this.raccoltePuntiList);
oos.close();
Toast.makeText(Globals.this, "DatiSalvati ", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Log.e("FileSave", "CDM - IOException", e);
Toast.makeText(this, "Errore saving file", Toast.LENGTH_LONG).show();
}
}
}
引用的类是:RaccoltaPunti.java
public class RaccoltaPunti {
private String nomeRaccolta;
private String nomePromoter;
private String numeroTessera;
private Long puntiPusseduti;
private String dataScadenzaPunti;
private String sitoWeb;
private String sitoWebUsername;
// constructor, getters and setters…….
}
最佳答案
- 如果要保存列表,
RaccoltaPunti
必须是Serializable
。
关于Java - 保存 ArrayList<ObjectType> : application crash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12109145/