编译器告诉我这个类Position
不可序列化。我不确定为什么我已经实现了serializble接口
private class Position
extends CommandParent implements CommandInterface, Serializable {
public Position(){
xVal = 10000;
yVal = 10000;
zVal = 10000;
iVal = null;
jVal = null;
}
@Override
public CommandType getCommandType(){return CommandType.POSITION;}
@Override
public String getKey(){return KEY_POSITION;}
}
这是界面
public interface CommandInterface {
CommandType getCommandType();
String getKey();
}
和超级
public class CommandParent implements Serializable {
// Data
protected Number xVal;
protected Number yVal;
protected Number zVal;
protected Number iVal;
protected Number jVal;
// Getters
protected Number getXcomponent(){ return xVal;}
protected Number getYcomponent(){ return yVal;}
protected Number getZcomponent(){ return zVal;}
protected Number getIcomponent(){ return iVal;}
protected Number getJcomponent(){ return jVal;}
// Setters
public void setXcomponent(Number x) { xVal = x; }
public void setYcomponent(Number y) { yVal = y; }
public void setZcomponent(Number z) { zVal = z; }
public void setIcomponent(Number i) { iVal = i; }
public void setJcomponent(Number j) { jVal = j; }
boolean xEnable = true;
boolean yEnable = true;
boolean zEnable = true;
}
这是我的序列化算法;我将类Position
作为接口类型传递给save
方法。
private void save(CommandInterface command) {
Log.v(TAG,"save");
Log.v(TAG,"saving : "+command.getKey());
try {
// Create a new file with an ObjectOutputStream.
File file = new File(mCtx.getFilesDir(), command.getKey());
ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(file));
// Write something in the file.
outputStream.writeObject(command); // <=== Throws the error
// Close and flush the stream.
outputStream.flush();
outputStream.close();
} catch (FileNotFoundException e) {
Log.d("Exception: ", Log.getStackTraceString(e));
} catch (IOException e) {
Log.d("Exception: ", Log.getStackTraceString(e));
}
}
此行的错误结果
// Write something in the file.
outputStream.writeObject(command); // <=== Throws the error
这是堆栈跟踪
Java.io.NotSerializableException: com.example.michael.gloevo1.ActivityGloEvoPage.GloEvoPageActivity
at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1344)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:959)
at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:360)
at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1054)
at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1384)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:959)
at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:360)
at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1054)
at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1384)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
at com.example.michael.gloevo1.ActivityGloEvoPage.CommandManager.save(CommandManager.java:252)
at com.example.michael.gloevo1.ActivityGloEvoPage.CommandManager.saveCurrentCommand(CommandManager.java:66)
at com.example.michael.gloevo1.ActivityGloEvoPage.GloEvoPageActivity.onPause(GloEvoPageActivity.java:123)
at android.app.Activity.performPause(Activity.java:6386)
at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1311)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3385)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3358)
at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3333)
at android.app.ActivityThread.-wrap13(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1358)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5451)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:1)
编译器告诉我这个类
Position
不可序列化。
不,不是。 JVM 在运行时通过异常告诉您:
Java.io.NotSerializableException: com.example.michael.gloevo1.ActivityGloEvoPage.GloEvoPageActivity
这是因为Position
是一个内部类,例如GloEvoPageActivity
,这意味着Position
实例拥有对封闭GloEvoPageActivity
实例的引用,不可序列化。
使Position
成为静态或顶级类,并解决出现的任何编译错误。
答案 1 :(得分:0)
我的解决方案是在写入对象
之前使GloEvoPageActivity
传入的全局上下文变量无效
private void save(CommandInterface command) {
Log.v(TAG,"save");
Log.v(TAG,"saving : "+command.getKey());
Log.v(TAG,"command is an instance of position : "+String.valueOf(command instanceof Position));
Log.v(TAG,"command is an instance of position : "+String.valueOf(command instanceof GloEvoPageActivity));
try {
// Create a new file with an ObjectOutputStream.
File file = new File(mCtx.getFilesDir(), command.getKey());
ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(file));
Context temp = mCtx;
mCtx = null;
// Write something in the file.
outputStream.writeObject(command);
mCtx = temp;
// Close and flush the stream.
outputStream.flush();
outputStream.close();
} catch (FileNotFoundException e) {
Log.d("Exception: ", Log.getStackTraceString(e));
} catch (IOException e) {
Log.d("Exception: ", Log.getStackTraceString(e));
}
}