[英]NotSerializableException threw by ObjectInputStream.readObject()
[英]ObjectInputStream.readObject() NotSerializableException
我在Android應用程序中使用ObjectOutputStream和ObjectInputStream保存和讀取保存在文件中的對象時遇到一些問題,我有兩個類Workout和Exercise,Workout包含一份Exercise列表。 這兩個類都實現了Serializable。 這是實際的代碼:
鍛煉
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;
}
}
保存類的當前實例沒有問題,但是當我嘗試讀回該對象時,拋出NotSerializableException。 以下是詳細信息和回溯:
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 }
我不明白我的錯誤是什么,我錯過了什么。 預先感謝您的幫助!
為了使對象可序列化,它的所有實例字段以及它們的所有實例字段都必須可序列化。 您的Workout
有
final Context context = MyApp.getContext();
Android的Context
對象不可序列化。 如果您遇到這種情況,因為您顯然只是將上下文存儲在靜態字段中,則只需為其刪除實例字段,然后在需要的地方直接使用MyApp.getContent()
。 不是說我寬容使用全局變量。
或者,正如@Bedla在評論中指出的那樣,
transient final Context context = MyApp.getContext();
您也可以用transient
標記字段,以表示它不應被序列化。 但是,當然,您必須為不反序列化該字段做好准備 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.