Java:使用原始类型序列化对象。获取错误java.io.NotSerializableException

时间:2012-09-16 14:19:23

标签: java serialization serializable

班级定义

private class classSerial implements Serializable {
    private static final long serialVersionUID = 1L;
    String vString;
    boolean vBool;
    long vLong;
    int vInt;

    classSerial(){
     //initialize members;
    }
}

我仍然收到错误:

Caused by: java.io.NotSerializableException: com.example.....

当我尝试在writeObject的对象上执行FileOutputStream(使用classSerial的对象)。


根据我的阅读,我使用的数据类型StringboolenlongintSerializable。所以,我不需要实现任何方法。

我在这里失踪的是什么?


logcat的

09-16 19:58:26.170: W/dalvikvm(4278): threadid=1: thread exiting with uncaught exception (group=0x40018560)
09-16 19:58:26.201: E/AndroidRuntime(4278): FATAL EXCEPTION: main
09-16 19:58:26.201: E/AndroidRuntime(4278): java.lang.IllegalStateException: Could not execute method of the activity
09-16 19:58:26.201: E/AndroidRuntime(4278):     at android.view.View$1.onClick(View.java:2165)
09-16 19:58:26.201: E/AndroidRuntime(4278):     at android.view.View.performClick(View.java:2506)
09-16 19:58:26.201: E/AndroidRuntime(4278):     at android.view.View$PerformClick.run(View.java:9112)
09-16 19:58:26.201: E/AndroidRuntime(4278):     at android.os.Handler.handleCallback(Handler.java:587)
09-16 19:58:26.201: E/AndroidRuntime(4278):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-16 19:58:26.201: E/AndroidRuntime(4278):     at android.os.Looper.loop(Looper.java:130)
09-16 19:58:26.201: E/AndroidRuntime(4278):     at android.app.ActivityThread.main(ActivityThread.java:3835)
09-16 19:58:26.201: E/AndroidRuntime(4278):     at java.lang.reflect.Method.invokeNative(Native Method)
09-16 19:58:26.201: E/AndroidRuntime(4278):     at java.lang.reflect.Method.invoke(Method.java:507)
09-16 19:58:26.201: E/AndroidRuntime(4278):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
09-16 19:58:26.201: E/AndroidRuntime(4278):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
09-16 19:58:26.201: E/AndroidRuntime(4278):     at dalvik.system.NativeStart.main(Native Method)
09-16 19:58:26.201: E/AndroidRuntime(4278): Caused by: java.lang.reflect.InvocationTargetException
09-16 19:58:26.201: E/AndroidRuntime(4278):     at java.lang.reflect.Method.invokeNative(Native Method)
09-16 19:58:26.201: E/AndroidRuntime(4278):     at java.lang.reflect.Method.invoke(Method.java:507)
09-16 19:58:26.201: E/AndroidRuntime(4278):     at android.view.View$1.onClick(View.java:2160)
09-16 19:58:26.201: E/AndroidRuntime(4278):     ... 11 more
09-16 19:58:26.201: E/AndroidRuntime(4278): Caused by: java.io.NotSerializableException: com.example.testing.serialActivity
09-16 19:58:26.201: E/AndroidRuntime(4278):     at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1535)
09-16 19:58:26.201: E/AndroidRuntime(4278):     at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1847)
09-16 19:58:26.201: E/AndroidRuntime(4278):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1689)
09-16 19:58:26.201: E/AndroidRuntime(4278):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1653)
09-16 19:58:26.201: E/AndroidRuntime(4278):     at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:1143)
09-16 19:58:26.201: E/AndroidRuntime(4278):     at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:413)
09-16 19:58:26.201: E/AndroidRuntime(4278):     at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1241)
09-16 19:58:26.201: E/AndroidRuntime(4278):     at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1575)
09-16 19:58:26.201: E/AndroidRuntime(4278):     at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1847)
09-16 19:58:26.201: E/AndroidRuntime(4278):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1689)
09-16 19:58:26.201: E/AndroidRuntime(4278):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1653)
09-16 19:58:26.201: E/AndroidRuntime(4278):     at com.example.testing.serialActivity.login(login_activity.java:277)
09-16 19:58:26.201: E/AndroidRuntime(4278):     at com.example.testing.serialtest.login(serialtest.java:34)
09-16 19:58:26.201: E/AndroidRuntime(4278):     ... 14 more

1 个答案:

答案 0 :(得分:3)

问题很可能是你有一个内部阶级。即它不是静态的,因此它引用了不可序列化的外部类。这就是检查的原因,以避免序列化你不想要的东西。

你会在堆栈跟踪中注意到它所抱怨的类是另一个类。

NotSerializableException: com.example.testingserialActivity

尝试制作课程

private static class ClassSerial implement Serializable