【发布时间】:2012-08-24 12:08:21
【问题描述】:
我需要一种方法来保存 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…….
}
【问题讨论】:
-
当它崩溃时你能发布堆栈跟踪吗?
-
异常告诉你究竟出了什么问题。不要只是忽略它,也不要只是将您看到的每个错误都归结为“应用程序崩溃”。
标签: java android serialization crash