The following exception occurs in the sequence when the result finds that the sequence is not successful in a non-static inner class:
Java.io.NotSerializableException:com.tang.sharedpreferencesdemo.MainActivity
W/system.err (2356): at Java.io.ObjectOutputStream.writeNewObject (objectoutputstream.java:1364)
W/system.err (2356): at Java.io.ObjectOutputStream.writeObjectInternal (objectoutputstream.java:1671)
W/system.err (2356): at Java.io.ObjectOutputStream.writeObject (objectoutputstream.java:1517)
W/system.err (2356): at Java.io.ObjectOutputStream.writeObject (objectoutputstream.java:1481)
W/system.err (2356): at Java.io.ObjectOutputStream.writeFieldValues (objectoutputstream.java:979)
W/system.err (2356): at Java.io.ObjectOutputStream.defaultWriteObject (objectoutputstream.java:368)
Through the data and analysis know:
assuming that a serializable object includes a reference to an object that is not serializable, the entire serialization operation fails and throws a notserializableexception .
Non-static inner classes have full access to all members of the external class, including instance fields and methods. To implement this behavior, a non-static inner class stores an implicit reference to an instance of an external class. Serialization requires that all member variables be Serializable, and now the outer class does not have implements Serializable, so the java.io.NotSerializableException exception is thrown.
The workaround:
1. Write the inner class as a static
2. Write the inner class separately. java file implements Serializable
Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.
Java.io.NotSerializableException