Java.io.NotSerializableException, even if I implement "Serializable"

I have a little problem: I wanted to test serialization on android (using eclipse) and found an example on how to do this. I know that I need to implement "Serializable" in the class that I want to serialize, I already did this and always get a java.io.NotSerializableException. Here is the code:

public void Button(View view) throws IOException, ClassNotFoundException {
ser test = new ser();
    test.x = 204;
    test.y = 2843;
    test.speed = 12;
    test.direction = 1343;
    test.a = 493;
    test.b = 2323;
    test.c = 29489;
    test.d = 394;

    byte[] arr = serialize(test);

    ser res = (ser) deserialize(arr);
}


public static byte[] serialize(Object o) { 
    ByteArrayOutputStream bos = new ByteArrayOutputStream();  
    try {   
        ObjectOutput out = new ObjectOutputStream(bos);      
        out.writeObject(o);                                       //This is where the Exception occurs
        out.close();     
        // Get the bytes of the serialized object    
        byte[] buf = bos.toByteArray();   
        return buf;    
    } catch(IOException ioe) { 
        Log.e("serializeObject", "error", ioe);           //"ioe" says java.io.NotSerializableException exception
        return null; 
    }  

}


public static Object deserialize(byte[] b) {  
        try {    
            ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(b));    
            Object object = in.readObject();    
            in.close();  
            return object;  
        } catch(ClassNotFoundException cnfe) {   
            Log.e("deserializeObject", "class not found error", cnfe);   
            return null;  
        } catch(IOException ioe) {  
            Log.e("deserializeObject", "io error", ioe);    
            return null; 
        } 
    } 

class ser implements Serializable {
    float x, y, speed, direction, a, b, c, d;
}

I hope you help me, I don’t know what I did wrong ...

Edit: my import:

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
+5
source share
7 answers

, - ? , java.io.NotSerializableException. , implemnent Serializable , Serializable (.. Serializable).

, :

- "java.io.NotSerializableException: NonSerializableClassName"

: Bozho, , - .

+7

, , , , , - , - . .

java-.

, , , sunw.io.Serializable, java.io.Serializable, io, .

+6

, . NotSerializableException? , , , , outter, . .

+3

. "ser" - ( , , "Activity", serilizable), , "ser" .

(.. , -), , .

+2

, , .

, , ser . Java , , .

, ser ser.java, .

0

. , .ser , . - () ArrayList . , . , , , .ser, List. , , . -ser , - .

0

, , , serialVersionUID, - private static final long serialVersionUID = 7536482295622776147L; , ( , ) , :

class ser implements Serializable {
    private static final long serialVersionUID = 7536482295622776147L;
    float x;
    float y;
    float speed;
    float direction;
    float a;
    float b;
    float c;
    float d;
}

Once again, I was a little late, but this question has thousands of views and you did not select an answer, so maybe this will help someone.

0
source

All Articles