Java 为对象引发Android NotSerializableException

Java 为对象引发Android NotSerializableException,java,android,serialization,Java,Android,Serialization,在我的android应用程序中,我使用文件存储许可证数据。我使用序列化对象。我创建了一个设备对象,并将文件详细信息读取到该对象中设备类实现可序列化 public class MyDevice implements Serializable {} 但在应用程序开始时,它将反序列化并存储在MyDevice对象中。我的反序列化对象方法如下所示 public MyDevice deserializeObject() { File SerialFile = new File(GeoTracke

在我的android应用程序中,我使用文件存储许可证数据。我使用序列化对象。我创建了一个设备对象,并将文件详细信息读取到该对象中设备类实现可序列化

public class MyDevice implements Serializable {}
但在应用程序开始时,它将反序列化并存储在MyDevice对象中。我的反序列化对象方法如下所示

public MyDevice deserializeObject() {

    File SerialFile = new File(GeoTrackerPaths.FILE_PATH);
    MyDevice AndDeviceIn = new MyDevice();

    if (SerialFile.exists()) {
        try {
            FileInputStream fileIn = new FileInputStream(GeoTrackerPaths.FILE_PATH);
            ObjectInputStream objInput = new ObjectInputStream(fileIn);
            AndDeviceIn = (MyDevice) objInput.readObject();
            objInput.close();
            fileIn.close();

        } catch (Exception e) {

            Log.i("TAG", "Exception during deserialization:" + e.getMessage());
            e.printStackTrace();
            System.exit(0);
        }
    }

    return AndDeviceIn;
}
DeviceActivator activate=new DeviceActivator();
activate.serializeObject(Activation.this, phoneModel, androidVersion, txtExe, exeKey, modeilID, tempKey, noLogin, expireDate, Activation_Status);
我的序列化代码

public void serializeObject(Context context, String phoneModel,
        String androidVersion, String executiveCode, String Key,
        String modelID, String tempKey, int noLogin, String expireDate, String Status) {

    try {
        MyDevice AndDeviceOut = new MyDevice(context, phoneModel,
                androidVersion, new Date(), executiveCode, Key, modelID,
                tempKey, noLogin, expireDate, Status);

        FileOutputStream fileOut = new FileOutputStream(
                GeoTrackerPaths.FILE_PATH);
        ObjectOutputStream objOutput = new ObjectOutputStream(fileOut);
        objOutput.writeObject(AndDeviceOut);
        objOutput.flush();
        objOutput.close();
        fileOut.close();

    } catch (Exception e) {
        Log.i("TAG", "Exception during serialization:" + e.getMessage());
        e.printStackTrace();
        System.exit(0);
    }
}
我叫它如下

public MyDevice deserializeObject() {

    File SerialFile = new File(GeoTrackerPaths.FILE_PATH);
    MyDevice AndDeviceIn = new MyDevice();

    if (SerialFile.exists()) {
        try {
            FileInputStream fileIn = new FileInputStream(GeoTrackerPaths.FILE_PATH);
            ObjectInputStream objInput = new ObjectInputStream(fileIn);
            AndDeviceIn = (MyDevice) objInput.readObject();
            objInput.close();
            fileIn.close();

        } catch (Exception e) {

            Log.i("TAG", "Exception during deserialization:" + e.getMessage());
            e.printStackTrace();
            System.exit(0);
        }
    }

    return AndDeviceIn;
}
DeviceActivator activate=new DeviceActivator();
activate.serializeObject(Activation.this, phoneModel, androidVersion, txtExe, exeKey, modeilID, tempKey, noLogin, expireDate, Activation_Status);
当我运行应用程序时,会引发以下异常

java.io.WriteAbortedException: Read an exception; 
java.io.NotSerializableException: com.geotracker.entity.MyDevice

如何修复此问题?

Android对象看起来不是可序列化的。您可以通过将
上下文
对象声明为transient来解决这个问题,您可以在JDK规范中阅读到。基本上,将字段标记为瞬态意味着它不会参与序列化

因此,在
MyDevice
中这样声明您的字段:

私有瞬态上下文


你应该可以走了

与异常无关,但为什么要创建新的MyDevice对象,然后将相同的引用分配给从文件中读取的对象?您的意思是“MyDevice and Devicein=new MyDevice();”不是吗。我是否应该创建一个MyDevice实例来存储对象…………示例反序列化代码看起来没有问题,执行序列化过程的代码可能有问题。你也可以发布那个部分吗?好的,我会发布序列化部分……我不相信Android
Context
对象是可序列化的。请尝试删除它,然后再次运行该过程,以确定这是否是问题所在