以下是我的班级。它简单地将一个序列化对象写入套接字输出流。我在writeObject行遇到了一个奇怪的错误。为什么呢?
W/System.err( 576): java.io.StreamCorruptedException
W/System.err( 576): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1712)
W/System.err( 576): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1665)
W/System.err( 576): at com.megaclient.activity.ControlActivity.sendDelta(ControlActivity.java:92)
W/System.err( 576): at com.megaclient.activity.ControlActivity.touchTouchPad(ControlActivity.java:68)
W/System.err( 576): at com.megaclient.activity.ControlActivity.access$0(ControlActivity.java:48)
W/System.err( 576): at com.megaclient.activity.ControlActivity$1.onTouch(ControlActivity.java:42)
W/System.err( 576): at android.view.View.dispatchTouchEvent(View.java:3762)
W/System.err( 576): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
W/System.err( 576): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
W/System.err( 576): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
W/System.err( 576): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
W/System.err( 576): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1671)
W/System.err( 576): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
W/System.err( 576): at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
W/System.err( 576): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1655)
W/System.err( 576): at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)
W/System.err( 576): at android.os.Handler.dispatchMessage(Handler.java:99)
W/System.err( 576): at android.os.Looper.loop(Looper.java:123)
W/System.err( 576): at android.app.ActivityThread.main(ActivityThread.java:4627)
W/System.err( 576): at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err( 576): at java.lang.reflect.Method.invoke(Method.java:521)
W/System.err( 576): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
W/System.err( 576): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
W/System.err( 576): at dalvik.system.NativeStart.main(Native Method)
我的班级
public class ControlActivity extends Activity {
...
public void somewhere() {
...
final Socket socket = new Socket(host, port);
sendDelta(socket, dx, dy);
....
}
public void sendDelta(final Socket socket, final int dx, final int dy) {
try {
final ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
final MegaPoint point = new MegaPoint(dx, dy);
oos.writeObject(point);
oos.close();
socket.close();
} catch (final StreamCorruptedException e) {
e.printStackTrace();
} catch (final IOException e) {
e.printStackTrace();
}
}
}
public class MegaPoint implements Serializable {
private final int x, y;
public MegaPoint(final int x, final int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}