在Serializable对象上获取NotSerializableException?

时间:2016-11-30 07:29:22

标签: java android serialization

我在运行抱怨我NotSerializableException课程的应用时遇到AbilityScore,但正如您所看到的,该课程及其所有成员都是{{1} (即使是枚举,即使我不认为这是必要的)。

这是我的代码:

Serializable

这是导致异常的方法......

public class AbilityScore implements Serializable {
    private Scores type;
    public Scores getScoreType() { return type; }

    private int value;
    public int getScoreValue() { return value; }
    public void setScoreValue(int v) { value = v; }
    public int getScoreModifier() { return ((int)(value / 2) - 5); }

    private boolean proficient;
    public boolean isProficient() { return proficient; }
    public void setProficient(boolean p) { proficient = p; }

    public AbilityScore(Scores t, int v) {
        type = t;
        value = v;
    }

    public enum Scores implements Serializable {
        STRENGTH, DEXTERITY, CONSTITUTION, INTELLIGENCE, WISDOM, CHARISMA;
    }
}

这里是其中一个文件的堆栈跟踪...

public void saveCharacter(Context context) {
    String filename = me.getFilename();

    FileOutputStream fout = null;
    ObjectOutputStream oos = null;
    try {
        //fout = new FileOutputStream(filename, false);
        fout = context.openFileOutput(filename, MODE_PRIVATE);
        oos = new ObjectOutputStream(fout);
        oos.writeObject(me);
        Toast.makeText(context, "Saved!", Toast.LENGTH_SHORT).show();

        if(oos != null) oos.close();
    } catch(Exception e) {
        Log.e(LOG_TAG, "ERROR: Problem saving character to file => " + filename);
        Toast.makeText(context, "Error saving character...", Toast.LENGTH_SHORT).show();
        e.printStackTrace();
    }
}

public CharacterInfo loadCharacter(Context context, String filename) {
    FileInputStream fin = null;
    ObjectInputStream ois = null;
    CharacterInfo myCharacter = null;
    try {
        //fin = new FileInputStream(filename);
        fin = context.openFileInput(filename);
        ois = new ObjectInputStream(fin);
        myCharacter = (CharacterInfo) ois.readObject();
        Toast.makeText(context, "Loaded!", Toast.LENGTH_SHORT);

        if(ois != null) ois.close();
    } catch(Exception e) {
        Log.e(LOG_TAG, "ERROR: Problem loading character from file => " + filename);
        Toast.makeText(context, "Error loading character...", Toast.LENGTH_SHORT);
        e.printStackTrace();
    }

    return myCharacter;
}

是的,11-30 02:45:11.285 27048-27048/? W/System.err: java.io.WriteAbortedException: Read an exception; java.io.NotSerializableException: edu.uml.android.adventurersarchive.character.AbilityScore 11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:778) 11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1983) 11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1940) 11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectInputStream.readNewArray(ObjectInputStream.java:1488) 11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:759) 11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1983) 11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1940) 11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectInputStream.readFieldValues(ObjectInputStream.java:1113) 11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:454) 11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1345) 11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1242) 11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1835) 11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:761) 11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1983) 11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1940) 11-30 02:45:11.285 27048-27048/? W/System.err: at edu.uml.android.adventurersarchive.GlobalState.loadCharacter(GlobalState.java:54) 11-30 02:45:11.285 27048-27048/? W/System.err: at edu.uml.android.adventurersarchive.LoadCharacterActivity.findCharacters(LoadCharacterActivity.java:47) 11-30 02:45:11.285 27048-27048/? W/System.err: at edu.uml.android.adventurersarchive.LoadCharacterActivity.onCreate(LoadCharacterActivity.java:32) 11-30 02:45:11.285 27048-27048/? W/System.err: at android.app.Activity.performCreate(Activity.java:6056) 11-30 02:45:11.285 27048-27048/? W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 11-30 02:45:11.285 27048-27048/? W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332) 11-30 02:45:11.285 27048-27048/? W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2441) 11-30 02:45:11.285 27048-27048/? W/System.err: at android.app.ActivityThread.access$800(ActivityThread.java:162) 11-30 02:45:11.285 27048-27048/? W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349) 11-30 02:45:11.285 27048-27048/? W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102) 11-30 02:45:11.285 27048-27048/? W/System.err: at android.os.Looper.loop(Looper.java:135) 11-30 02:45:11.285 27048-27048/? W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5431) 11-30 02:45:11.285 27048-27048/? W/System.err: at java.lang.reflect.Method.invoke(Native Method) 11-30 02:45:11.285 27048-27048/? W/System.err: at java.lang.reflect.Method.invoke(Method.java:372) 11-30 02:45:11.285 27048-27048/? W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:914) 11-30 02:45:11.285 27048-27048/? W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707) 11-30 02:45:11.285 27048-27048/? W/System.err: Caused by: java.io.NotSerializableException: edu.uml.android.adventurersarchive.character.AbilityScore 11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1344) 11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651) 11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497) 11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461) 11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectOutputStream.writeNewArray(ObjectOutputStream.java:1185) 11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1642) 11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497) 11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461) 11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:959) 11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:360) 11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1054) 11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1384) 11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651) 11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497) 11-30 02:45:11.285 27048-27048/? W/System.err: at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461) 11-30 02:45:11.285 27048-27048/? W/System.err: at edu.uml.android.adventurersarchive.GlobalState.saveCharacter(GlobalState.java:35) 11-30 02:45:11.285 27048-27048/? W/System.err: at edu.uml.android.adventurersarchive.CreateCharacterActivity.submitCharacter(CreateCharacterActivity.java:73) 11-30 02:45:11.285 27048-27048/? W/System.err: at java.lang.reflect.Method.invoke(Native Method) 11-30 02:45:11.285 27048-27048/? W/System.err: at java.lang.reflect.Method.invoke(Method.java:372) 11-30 02:45:11.285 27048-27048/? W/System.err: at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284) 11-30 02:45:11.285 27048-27048/? W/System.err: at android.view.View.performClick(View.java:4791) 11-30 02:45:11.285 27048-27048/? W/System.err: at android.view.View$PerformClick.run(View.java:19991) 11-30 02:45:11.285 27048-27048/? W/System.err: at android.os.Handler.handleCallback(Handler.java:739) 11-30 02:45:11.285 27048-27048/? W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95) 11-30 02:45:11.285 27048-27048/? W/System.err: ... 6 more ,其中的每个成员都是CharacterInfo。它特别抱怨Serializable。这里发生了什么?

2 个答案:

答案 0 :(得分:0)

堆栈跟踪显示,在将NotSerializableException添加到其定义之前,编写对象时必须有implements Serializable

您需要删除序列化文件并重新开始。

答案 1 :(得分:0)

问题出在这里:

“私人分数类型;  public 得分 getScoreType(){返回类型; }“

您必须在“分数”中实现Serializable。