Java 反序列化的对象字段为空
我已成功序列化自定义对象,但反序列化时会发生以下情况: -自定义对象不为空 -所有字段均为空 我知道我已经成功地序列化了我的自定义对象,因为我已经读取了序列化文件,它看起来很好 这是我的密码:Java 反序列化的对象字段为空,java,android,serialization,Java,Android,Serialization,我已成功序列化自定义对象,但反序列化时会发生以下情况: -自定义对象不为空 -所有字段均为空 我知道我已经成功地序列化了我的自定义对象,因为我已经读取了序列化文件,它看起来很好 这是我的密码: public class Preferences implements Serializable { private static Preferences instance; public static final long serialVersionUID = 3358037972944864859L
public class Preferences implements Serializable {
private static Preferences instance;
public static final long serialVersionUID = 3358037972944864859L;
public String accessToken;
protected Object readResolve() {
return getInstance();
}
private Preferences() {
}
private synchronized static void synchronize() {
if(instance == null) {
instance = new Preferences();
}
}
public static Preferences getInstance() {
if(instance == null) {
Preferences.synchronize();
}
return instance;
}
public void save(File file) {
try {
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream out = new ObjectOutputStream(fos);
Preferences tempInstance = Preferences.getInstance();
out.writeObject(tempInstance);
out.close();
fos.close();
}catch(IOException e) {
e.printStackTrace();
}
}
public void load(File file) {
try {
FileInputStream fis = new FileInputStream(file);
ObjectInputStream in = new ObjectInputStream(fis);
if(file.length() > 0) {
Preferences tempInstance = (Preferences) in.readObject();
Log.e("", String.valueOf(tempInstance == null)); //prints FALSE
Log.e("", String.valueOf(tempInstance.accessToken == null)); //prints TRUE
}
in.close();
fis.close();
}catch(IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
以下是我的测试代码:
public class CustomActivity extends AppCompatActivity {
private File dir = new File(Environment.getExternalStorageDirectory(), ".app");
private File backup = new File(dir, "backup.ser");
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
Log.e("APPLICATION", "START");
super.onCreate(savedInstanceState);
if(this instanceof ActivityLogin) {
if(!dir.exists()) {
dir.mkdirs();
}
Preferences.getInstance().load(backup);
}
}
@Override
protected void onUserLeaveHint() {
super.onUserLeaveHint();
try {
backup.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
Preferences.getInstance().save(backup);
Log.e("APPLICATION", "STOP");
}
}
对可能出现的问题有什么想法吗 您的类中有此方法:
protected Object readResolve() {
return getInstance();
}
这告诉序列化机制:无论何时反序列化首选项实例,都将其替换为getInstance()
返回的实例。因此,如果您调用load(),并且首选项的实例具有null accessToken,那么反序列化的首选项也将具有null accessToken,因为它们是相同的对象
加
到您的日志语句(或您在android中使用的任何日志),您将看到。您没有对read对象执行任何操作。load方法应该是静态的,并且应该返回反序列化的实例
instance
和this
是两个不同的、不相关的对象。在load()完成后,我正在对instance对象执行一些操作,但在load()中,instance不为null,而其所有字段均为null。请看我的编辑@JBNizetPost提供了一个复制错误的完整的最小示例。我们不知道你在序列化和反序列化什么。@fabian我已经解决了,但同样的问题。请参阅“我的编辑”。@jbnize未发布完整的代码。是。但我也会停止使用可怕的单例反模式。只需将load方法设置为静态,使其返回反序列化的首选项,并将其存储在变量中。如果我创建setInstance(),将instance field和pass load()设置为一个参数,会怎么样呢?我已经创建了setInstance(),就像我上面所说的,并遵循您的建议,字段不为null。我还想学习一些单例模式/全局类的替代方案。非常感谢你!
System.out.println(tempInstance == this);