Java - 保存 ArrayList<ObjectType> : application crash

标签 java android serialization crash

我需要一种方法来保存对象的 ArrayList。我在网站上浏览过类似的问题并且我(似乎 ;-) 已经实现了我发现的东西,但是我遇到了两个问题:

  1. 如果我将类定义为 Serializable 并放置构造函数,它会在启动时崩溃
  2. 否则不保存数组

你能帮忙吗?我正在为一个志愿者的项目开发代码,但我被困住了......

非常感谢您。

我的应用程序具有以下定义的类: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/

相关文章:

java - 合并文件时BufferOutputStream写入零字节

java - Android:如何在 webview Activity 中显示 Youtube channel ?

Android HTTPS SNI 支持使用 SSLCertificateSocketFactory

Python/Arduino串口通信

java - 找回密码Tomcat认证

java - 将 Jackson 注释添加到 swagger 生成的类

java - 为 java.util.Maps 自定义 Jersey JSON 编码

fb sdk3.0中的Android Facebook登录错误

c# - InvalidDataContractException 是无效的集合类型,因为它具有 DataContractAttribute

c# - 为什么 JsonConvert.DeserializeObject 不起作用?