java.io.NotSerializableException while writing Serializable object to external storage?

前端 未结 1 908
没有蜡笔的小新
没有蜡笔的小新 2020-12-15 22:25

friends,

i am using following code to write Serializable object to external storage.

it throws me error java.io.NotSerializableException even my object is se

相关标签:
1条回答
  • 2020-12-15 23:07

    This fails due to the Context field in your class. Context objects are not serializable.

    Per the Serializable documentation - "When traversing a graph, an object may be encountered that does not support the Serializable interface. In this case the NotSerializableException will be thrown and will identify the class of the non-serializable object."

    You can either remove the Context field entirely, or apply the transient attribute to the Context field so that it is not serialized.

    public class MyClass implements Serializable 
    {
        ...
        public transient Context myContext;
        ...
    }
    
    0 讨论(0)
提交回复
热议问题