AndroidアプリケーションにObjectOutputStreamとObjectInputStreamを含むファイルに保存されたオブジェクトを保存したり読み込んだりするときに問題が発生しました 私はWorkoutとExerciseの2つのクラスを持っていますが、WorkoutにはExerciseのリストが含まれています。どちらのクラスもSerializableを実装しています。ここでは実際のコードです:ObjectInputStream.readObject()NotSerializableException
ワークアウト
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 }
私の間違いや私が紛失しているものは理解できません。ご協力いただきありがとうございます!
コンテキストをシリアル化できません。一時的にする。 '一時的なコンテキストContext = MyApp.getContext();' – Bedla