发生NotSerializableException

时间:2012-06-02 08:10:14

标签: java

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)

任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:8)

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

你可以通过使它静止来解决这个问题:

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;
    }
}

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

答案 1 :(得分:0)

每当您收到java.io.NotSerializableException异常错误时。确保您implements Serializable界面和"将变量定义为静态"