我有来自Apache Commons的这个类,我用来存储国际象棋数据。我正在保存ChessPiece[][]
,其中ChessPiece
是一个带有Rook
Pawn
等子类的抽象类,每个子类都有自己的坐标和其他数据。这是代码:
public class ObjectSerializer {
private static final Log log = LogFactory.getLog(ObjectSerializer.class);
public static String serialize(Serializable obj) throws IOException {
if (obj == null) return "";
try {
ByteArrayOutputStream serialObj = new ByteArrayOutputStream();
ObjectOutputStream objStream = new ObjectOutputStream(serialObj);
objStream.writeObject(obj);
objStream.close();
return encodeBytes(serialObj.toByteArray());
} catch (Exception e) {
throw WrappedIOException.wrap("Serialization error: " + e.getMessage(), e);
}
}
public static Object deserialize(String str) throws IOException {
if (str == null || str.length() == 0) return null;
try {
ByteArrayInputStream serialObj = new ByteArrayInputStream(decodeBytes(str));
ObjectInputStream objStream = new ObjectInputStream(serialObj);
return objStream.readObject();
} catch (Exception e) {
throw WrappedIOException.wrap("Deserialization error: " + e.getMessage(), e);
}
}
public static String encodeBytes(byte[] bytes) {
StringBuffer strBuf = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
strBuf.append((char) (((bytes[i] >> 4) & 0xF) + ((int) 'a')));
strBuf.append((char) (((bytes[i]) & 0xF) + ((int) 'a')));
}
return strBuf.toString();
}
public static byte[] decodeBytes(String str) {
byte[] bytes = new byte[str.length() / 2];
for (int i = 0; i < str.length(); i+=2) {
char c = str.charAt(i);
bytes[i/2] = (byte) ((c - 'a') << 4);
c = str.charAt(i+1);
bytes[i/2] += (c - 'a');
}
return bytes;
}
}
我正在测试它,我只是序列化然后反序列化一个棋子
String r = "";
ChessPiece c = null;
try {
r = ObjectSerializer.serialize(currentBoardState[0][0].realcopyPiece());//Returns the `ChessPiece` at the cell
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
c = (ChessPiece) ObjectSerializer.deserialize(r);
System.out.println(c.x);//NO MATTER WHAT I PRINT ITS A NULL POINTER
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
这是我的logcat
12-06 07:25:52.880: E/AndroidRuntime(3888): FATAL EXCEPTION: main
12-06 07:25:52.880: E/AndroidRuntime(3888): java.lang.NullPointerException
12-06 07:25:52.880: E/AndroidRuntime(3888): at com.example.chess.InGameActivity$MyUndoButtonListener.onTouch(InGameActivity.java:513)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.View.dispatchTouchEvent(View.java:7379)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1954)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1954)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1954)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1954)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1954)
12-06 07:25:52.880: E/AndroidRuntime(3888): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1966)
12-06 07:25:52.880: E/AndroidRuntime(3888): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1418)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.app.Activity.dispatchTouchEvent(Activity.java:2424)
12-06 07:25:52.880: E/AndroidRuntime(3888): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1914)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.View.dispatchPointerEvent(View.java:7564)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3883)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3778)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3483)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3540)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5419)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5399)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5370)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5493)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:182)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.os.MessageQueue.nativePollOnce(Native Method)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.os.MessageQueue.next(MessageQueue.java:132)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.os.Looper.loop(Looper.java:124)
12-06 07:25:52.880: E/AndroidRuntime(3888): at android.app.ActivityThread.main(ActivityThread.java:5103)
12-06 07:25:52.880: E/AndroidRuntime(3888): at java.lang.reflect.Method.invokeNative(Native Method)
12-06 07:25:52.880: E/AndroidRuntime(3888): at java.lang.reflect.Method.invoke(Method.java:525)
12-06 07:25:52.880: E/AndroidRuntime(3888): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-06 07:25:52.880: E/AndroidRuntime(3888): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-06 07:25:52.880: E/AndroidRuntime(3888): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
根据用户的评论,每个ChessPiece都有一个位图
Java中的Bitmaps
不是serializable
在这些Bitmap对象之前添加transient
关键字,或者如果要序列化位图,请查看以下链接以获取更多帮助
Serializing and De-Serializing android.graphics.Bitmap in Java
android how to save a bitmap - buggy code