Android serialization: ImageView

Posted by embo on Stack Overflow See other posts from Stack Overflow or by embo
Published on 2010-03-24T21:55:33Z Indexed on 2010/03/24 23:03 UTC
Read the original article Hit count: 360

Filed under:
|
|
|

I have a simple class:

public class Ball2 extends ImageView implements Serializable {

    public Ball2(Context context) {
        super(context);
    }
}

Serialization ok:

private void saveState() throws IOException {

        ObjectOutputStream oos = new ObjectOutputStream(openFileOutput("data", MODE_PRIVATE));
        try {
            Ball2 data = new Ball2(Game2.this);

            oos.writeObject(data);
            oos.flush();

        } catch (Exception e) {
            Log.e("write error", e.getMessage(), e);
        } finally {
            oos.close();
        }
    }

But deserealization

private void loadState() throws IOException {
        ObjectInputStream ois = new ObjectInputStream(openFileInput("data"));
        try {
            Ball2 data = (Ball2) ois.readObject();
        } catch (Exception e) {
            Log.e("read error", e.getMessage(), e);
        } finally {
            ois.close();
        }
    }

fail with error:

03-24 21:52:43.305: ERROR/read error(1948): java.io.InvalidClassException: android.widget.ImageView; IllegalAccessException

How deserialize object correctly?

© Stack Overflow or respective owner

Related posts about android

Related posts about serialization