java - ObjectInputStream.readObject() NotSerializedException

标签 java android

我在 Android 应用程序中使用 ObjectOutputStream 和 ObjectInputStream 保存和读取保存在文件中的对象时遇到一些问题 我有两个类(class)“锻炼”和“锻炼”,“锻炼”除其他外还包含“锻炼”列表。这两个类都实现了可序列化。这是实际的代码:

锻炼

package com.mycompany.myapp;

public class Workout implements Serializable{

private String _name;
protected ArrayList<Exercise> _list = new ArrayList<Exercise>();
private static final long serialVersionUID = 1L;
private File _fileSave;
final Context context = MyApp.getContext();

Workout(){
    _name = "Empty";
    _list = null;
 }

 Workout(String n, ArrayList<Exercise> e){
     _name = n;
     _list.addAll(e);
 }

 Workout(File fs){
     ObjectInputStream ois;
     _fileSave = fs;
     try{
         ois = new ObjectInputStream(new FileInputStream(fs));
         Workout n = (Workout) ois.readObject();   //here it fails and throws the exception
         ois.close();
         _name = n._name;
         _list = n._list;
     }catch(IOException e){ 
        e.getCause();
     }catch(ClassNotFoundException e) {
     }
 }

 protected void save(){                                 
    ObjectOutputStream oos;
    _fileSave = new File(context.getFilesDir(), _name + ".w");
    try{
        oos = new ObjectOutputStream(new FileOutputStream(_fileSave));
        oos.writeObject(this);
        oos.close();
    }
    catch(Exception e){
        e.getCause();
    }
 }
 ...

锻炼

public class Exercise implements Serializable{

protected String _name;
protected int _weight;
protected int _reps;
protected int _sets;
protected int _pause;
protected int _duration;
private static final long serialVersionUID = 1L;

Exercise(){
    _name = "New Empty Exercise";
    _weight = 0;
    _reps = 0;
    _sets = 0;
    _pause = 0;
}

Exercise(String n, int w, int r, int s, int d, int p){
    _name = n;
    _weight = w;
    _reps = r;
    _sets = s;
    _duration = d;
    _pause = p;
}

}

我保存类的当前实例没有问题,但是当我尝试读回对象时,会抛出 NotSerializedException 。以下是详细信息和回溯:

cause = java.io.NotSerializableException: com.mycompany.myapp.MyApp

backtrace = { long[44]@b77201, class java.io.ObjectInputStream, class java.io.ObjectInputStream, class java.io.ObjectInputStream, class java.io.ObjectInputStream, class java.io.ObjectInputStream, class java.io.ObjectInputStream, class com.mycompany.myapp.Workout$0$debug, class com.mycompany.myapp.Workout, class com.mycompany.myapp.Menu$0$debug, class com.mycompany.myapp.Menu, class android.app.Activity, class android.app.Instrumentation, class android.app.ActivityThread, class android.app.ActivityThread, class android.app.ActivityThread, class android.app.ActivityThread$H, class android.os.Handler, class android.os.Looper, class android.app.ActivityThread, class java.lang.reflect.Method, class com.android.internal.os.Zygote$MethodAndArgsCaller, class com.android.internal.os.ZygoteInit }

我不明白我的错误是什么或我错过了什么。预先感谢您的帮助!

最佳答案

要使对象可序列化,它的所有实例字段及其所有实例字段都必须可序列化,依此类推。您的锻炼有,

final Context context = MyApp.getContext();

Android 的 Context 对象不可序列化。如果您的情况如此,由于您显然只是将上下文存储在静态字段中,因此只需删除它的实例字段并直接在需要的地方使用 MyApp.getContent() 即可。并不是说我容忍使用全局变量。

或者,正如@Bedla 在评论中指出的,

transient final Context context = MyApp.getContext();

您也可以使用 transient 标记字段,以表示它不应被序列化。但当然,您必须准备该字段不会被反序列化。

关于java - ObjectInputStream.readObject() NotSerializedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47600857/

相关文章:

java - org.hibernate.exception.JDBCConnectionException : Could not open connection exception in MySQL

android - 希望构建一个同步到 App Engine 数据存储区(python)的 Android 应用程序

android - 保存应用程序状态

java - 如何使用 Java 运行带有通配符的 unix/shell 命令?

java - 在运行时用 Java 合成新方法?

java - 需要帮助理解 getAcceptedIssuers 方法

android - 如果我从 Activity 中的 onCreate startService,finish() Activity 然后再次启动应用程序,会发生什么情况?

java - 将按钮的图像设置为应用程序图标

java - 由于字符串格式,在 android 中进行 http 调用时出现问题 - 索引 81 处的查询中存在非法字符

java - 在Java框架中打开程序