- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在我的 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/
class NotSerializable {} class MyClass implements Serializable { private NotSerializable field; /
尝试序列化 Lot 对象并将其发送到套接字。获取错误: java.io.NotSerializableException: com.server.ClientServiceThread 为什么? pu
我们在集群 QA 环境中遇到以下错误。我们在 linux 上运行 Weblogic 10.3.3.0。 #### > 0) { message.append(", Name "); }
在我的最后一个问题没有得到答案后,我将这件事重新表述为最简单的形式。 1 个按钮、1 个点击监听器、1 个可序列化对象和 1 个用于输出可序列化对象的子例程。此代码基于我在 stackoverflow
我正在尝试通过套接字连接发送定制对象。该类实现了可序列化,但当我尝试将对象写入套接字时,构造函数仍然抛出 NotSerializableException。我将在下面发布相关代码: public cl
基本上,我编写了一个在屏幕上绘制形状并将每个形状保存到 ArrayList 中的程序。我想做的是弄清楚如何将 ArrayList 保存到一个文件中,以便我可以稍后调用它并编辑已经存在的形状。 所以我一
java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: java.lang.reflect.
一段时间以来,我一直在尝试用序列化解决我的问题,不幸的是我迷路了。如果有人可以帮助我找到问题,我将不胜感激。我已经在每个类中实现了 Serializable。 已解决:问题是,一个类实现了 Seria
我正在尝试使用对象输出流将我制作的对象写入文件,每当我运行代码时,它都会抛出 NotSerializableException。如果您看到我做错了什么,请告诉我。 保存方法: public stati
我已经使用 OpenSSO 提供的 SAML 库实现了一个安全模块,但是我的服务器没有正确关闭并出现异常: java.io.NotSerializableException: com.sun.iden
我在社交网站上工作,我正在使用 JSF 2.2我有一些带有 View 范围的bean,在这些bean服务中注入(inject)了。 我有时在 tomcat 中遇到著名的 NotSerializable
java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException 为什么在启动我的服务器 Tomcat
我有一个 Activity,其中有一个私有(private)类数据。在 onSaveInstanceState 方法中,我尝试将此保存为 Data 的一个实例。这是我的整个 Activity : pu
我的两个类也设计为创建一个 StudentData 对象数组(姓名、出生日期和 ID),其中包括一个 toString 覆盖以打印出所有变量。然后序列化数组并将其保存到名为 studentdata.t
这是我的小类: import java.io.Serializable; public abstract class SerializableCallback extends Callback imp
在我的 Spark 代码中,我试图从一个 csv 文件创建一个 IndexedRowMatrix。但是,我收到以下错误: Exception in thread "main" org.apache.s
我编写了一个 GUI,它使用 RMI 与服务器应用程序连接。因为 GUI 必须向在线用户显示一个线程刷新包含“在线用户列表”的 JLabel。有时我在运行时遇到这个异常: Caused by: jav
我有一个过滤项目的界面: public interface KeyValFilter extends Serializable { public static final long seria
我正在处理 jenkinsfile 并且在第三阶段遇到异常: an exception which occurred: in field com.cloudbees.groovy.cps.impl.B
我在 JSF 2 中使用 spring 3 我用 spring bean 替换了 JSF 管理的 bean,方法是在 bean 之上添加: @Component("mybean") @Scope("s
我是一名优秀的程序员,十分优秀!