序列化完全不同的东西时,Activity的NotSerializableException

时间:2015-06-30 20:09:03

标签: java android android-activity serialization

我有一个Android Activity(CashFlowActivity),可以让用户创建一些数据。当他们离开活动时,我检查数据是否已被更改,如果是这样,将其保存到我的数据库中。

保存到数据库时,我将数据结构序列化并将其作为字节[]存储在blob中。

我的数据结构是一个扩展ArrayList的CashflowSeries。 CashflowSeries和CashFlow都是可序列化的。奇怪的是,我得到的NotSerializeableException抱怨CashFlowActivity。

它发生在CashFlow.writeObject()的oos.defaultWriteObject();上,这也很令人困惑。

以下是异常的堆栈跟踪:

06-30 19:48:42.764  13555-13555/com.inadaydevelopment.cashcalculator E/serializeObject﹕ error
    java.io.NotSerializableException: com.inadaydevelopment.cashcalculator.CashFlowActivity
            at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1364)
            at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
            at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
            at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
            at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:979)
            at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:368)
            at com.inadaydevelopment.cashcalculator.CashFlow.writeObject(CashFlow.java:37)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1053)
            at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
            at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
            at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
            at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
            at java.util.ArrayList.writeObject(ArrayList.java:644)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1053)
            at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
            at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
            at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
            at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
            at com.inadaydevelopment.cashcalculator.DBAdapter.serializeObject(DBAdapter.java:1428)
            at com.inadaydevelopment.cashcalculator.DBAdapter.saveCashFlowSeries(DBAdapter.java:505)
            at com.inadaydevelopment.cashcalculator.CashFlowActivity.saveSeries(CashFlowActivity.java:175)

以下是CashFlowSeries,它是我尝试序列化的顶级对象:

public class CashFlowSeries extends ArrayList<CashFlow> implements Serializable {

    private static final long serialVersionUID = 2665343990201574019L;

    private double numPeriodsPerYear;

    private Formatter formatter;
    private CashFlowSeriesDelegate delegate;


    private void writeObject(ObjectOutputStream oos) throws IOException{
        oos.defaultWriteObject();

        oos.writeDouble(this.numPeriodsPerYear);
        oos.writeInt(this.size());

        for(CashFlow cashFlow : this) {
            oos.writeObject(cashFlow);
        }

    }
    //... more stuff
}

以下是CashFlow类,它包含在CashFlowSeries列表中,并且是抛出异常的地方:

public class CashFlow implements Serializable {

    private static final long serialVersionUID = 7315728302372918920L;

    private static final String TAG = "CashFlow";

    private CashFlowSeries series;

    private String amountString;
    private String numRepetitionsString;

    private boolean isValidAmount;
    private boolean isValidNumRepetitions;

    private Formatter formatter;
    private CashFlowSeriesDelegate cashFlowSeriesDelegate;

    public CashFlow() {
        this.formatter = Formatter.getInstance();
        this.amountString = "";
        this.numRepetitionsString = "";
    }



    private void writeObject(ObjectOutputStream oos) throws IOException {
        oos.defaultWriteObject(); // EXCEPTION THROWN HERE

        oos.writeObject(this.amountString);
        oos.writeObject(this.numRepetitionsString);

        oos.writeBoolean(this.isValidAmount);
        oos.writeBoolean(this.isValidNumRepetitions);
    }

    // ... more stuff
}

P

ublic static byte[] serializeObject(Object o) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    try {
        ObjectOutput out = new ObjectOutputStream(bos);
        out.writeObject(o);
        out.close();

        // Get the bytes of the serialized object
        byte[] buf = bos.toByteArray();

        return buf;
    } catch (IOException ioe) {
        Log.e("serializeObject", "error", ioe);

        return null;
    }
}

我不知道为什么要尝试序列化这个活动..还是只是一个红鲱鱼?

0 个答案:

没有答案