gpt4 book ai didi

java - Android NotSerializableException 为对象引发

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:46:11 24 4
gpt4 key购买 nike

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

public class MyDevice implements Serializable {}

但在应用程序开始时,它会反序列化 并存储在 MyDevice 对象中。我的deserializeObject 方法如下。

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;
}

我的序列化代码

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);
}
}

我这样调用它。

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 Context对象是可序列化的。您可以通过将 Context 对象声明为 transient 来解决此问题,您可以在 JDK 规范的此处阅读:Link .基本上将一个字段标记为 transient 意味着它不会参与序列化

所以在 MyDevice 中声明你的字段:

private transient Context上下文;

你应该可以开始了!

关于java - Android NotSerializableException 为对象引发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21084884/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com