NotSerializableException:不确定原因是什么

时间:2014-01-13 08:21:27

标签: java android serialization

以下是我认为与此问题相关的代码的一部分。

//Enveloping class
public class BackupSMS implements Serializable{

transient Cursor cursor;
String vfile;
transient Context mContext;
private final String TAG = "BackupSMS";
SMSBackup smsBackup;
transient FileOutputStream mFileOutputStream;

public BackupSMS(Context context, String _vfile) {
    mContext = context;
    vfile = _vfile;
    smsBackup = new SMSBackup();
}

内部课程:

public class SMSBackup implements Serializable{
public ArrayList<SMSObject> SMSList;
public SMSBackup() {
    SMSList = new ArrayList<SMSObject>();
}
}

public class SMSObject implements Serializable{
private String id;
private String addr;
private String msg;
.... (more private strings and getter setter methods for them)
}

//method of enveloping class.
public void writeSMSList(){
......
    mFileOutputStream =  mContext.openFileOutput(vfile, Context.MODE_PRIVATE);
    ObjectOutputStream oos = new ObjectOutputStream(mFileOutputStream);
    //smsBackup is an object of SMSBackup class created earlier.
oos.writeObject( smsBackup );
oos.close();
.......
}

我似乎没有在图片中找到任何不可序列化的对象。任何出错的建议都会有所帮助。

编辑:我在堆栈跟踪上看起来不够近。下面是我获得的堆栈跟踪,但它仍然没有让我更接近解决方案。有什么建议吗?

java.io.NotSerializableException: android.content.ContentResolver$CursorWrapperInner
    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 java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1074)
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.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:979)
at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:368)
at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1074)
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)

....还有更多这样的台词。

1 个答案:

答案 0 :(得分:0)

如果嵌套类中没有直接引用其包含实例的内容(例如,通过访问非静态字段或调用包含类的非静态方法),那么它们可能不需要是内部类所有。如果您将类标记为static,那么它们将不会保留对容器实例的引用,因此不会尝试序列化整个对象图。