gpt4 book ai didi

java - ObjectInputStream.readObject() NotSerializedException

转载 作者:行者123 更新时间:2023-12-02 02:27:29 27 4
gpt4 key购买 nike

我在 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com