2
public class move implements Serializable
{
    private static final long serialVersionUID = 6696031872468154516L;
    public move(int a,int b){
        x=a;
        y=b;
    }
    int x,y;
}

我通过 ObjectOutputStream 发送这个 - 只使用 out.writeObject(params[0]);

当我看到 LogCat 时,它说:

06-02 16:09:48.750: W/System.err(6060): java.io.NotSerializableException: com.SWAP.AndTablet.MainActivity
06-02 16:09:48.750: W/System.err(6060):     at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1364)
06-02 16:09:48.750: W/System.err(6060):     at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
06-02 16:09:48.750: W/System.err(6060):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
06-02 16:09:48.750: W/System.err(6060):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
06-02 16:09:48.750: W/System.err(6060):     at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:979)
06-02 16:09:48.750: W/System.err(6060):     at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:368)
06-02 16:09:48.750: W/System.err(6060):     at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1074)
06-02 16:09:48.750: W/System.err(6060):     at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
06-02 16:09:48.750: W/System.err(6060):     at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
06-02 16:09:48.754: W/System.err(6060):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
06-02 16:09:48.754: W/System.err(6060):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
06-02 16:09:48.754: W/System.err(6060):     at com.SWAP.AndTablet.MainActivity$MouseMoveSendTask.doInBackground(MainActivity.java:59)
06-02 16:09:48.754: W/System.err(6060):     at com.SWAP.AndTablet.MainActivity$MouseMoveSendTask.doInBackground(MainActivity.java:1)
06-02 16:09:48.754: W/System.err(6060):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
06-02 16:09:48.754: W/System.err(6060):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
06-02 16:09:48.754: W/System.err(6060):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-02 16:09:48.754: W/System.err(6060):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
06-02 16:09:48.757: W/System.err(6060):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
06-02 16:09:48.757: W/System.err(6060):     at java.lang.Thread.run(Thread.java:856)

谁能帮我?

4

2 回答 2

8

我怀疑这是一个嵌套类,这意味着对外部类有一个隐式引用,即使你不使用它。因此,如果您的类嵌套在 中,即使您不想MainActivity序列化,您最终也会尝试序列化封闭的实例。不是导致错误的可序列化(几乎肯定不应该)。MainActivityMainActivity

您可以通过将其设为静态来解决此问题:

public static class Move implements Serializable {
    private final int x, y;
    private static final long serialVersionUID = 6696031872468154516L;

    public Move(int a,int b) {
        x = a;
        y = b;
    }
}

如果它不是嵌套类,那么我怀疑问题不在于您向我们展示的代码。

于 2012-06-02T08:16:15.143 回答
0

每当您收到java.io.NotSerializableException异常错误时。确保你implements Serializable接口并“将变量定义为静态”

于 2015-09-30T15:48:52.893 回答