java–Android NotSerializableException为一个对象引发

java–Android NotSerializableException为一个对象引发,第1张

概述在我的Android应用程序中,我使用文件来存储许可证数据.我使用Serialize对象.我创建一个Device对象并将文件详细信息读入该对象. Device类实现Serializable.public class MyDevice implements Serializable {} 但是在应用程序开始时,它反序列化并存储在MyDevice对象中.我的de

在我的Android应用程序中,我使用文件来存储许可证数据.我使用Serialize对象.我创建一个Device对象并将文件详细信息读入该对象. 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,txtExe,exeKey,modeilID,Activation_Status);

当我运行应用程序时,会引发异常.

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

我怎样才能解决这个问题?最佳答案它看起来不像AndroID Context对象是可序列化的.您可以通过将Context对象声明为瞬态来解决此问题,您可以在JDK规范中阅读此内容:Link.基本上将字段标记为瞬态意味着它不会参与序列化

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

私有瞬态上下文;

你应该好好去!

总结

以上是内存溢出为你收集整理的java – Android NotSerializableException为一个对象引发全部内容,希望文章能够帮你解决java – Android NotSerializableException为一个对象引发所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1140391.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存