如何在android中序列化视图对象

时间:2017-05-20 00:29:38

标签: java android

我有一个视图对象需要序列化才能存储在数据库中,然后再检索

public class MachineView extends View implements Serializable {

    String name;
    int age;

    public MachineView(String name, int age, Context context) {
        super(context);

        this.name = name;
        this.age = age;
    }



    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);



        int x = getWidth();
        int y = getHeight();
        int radius;
        radius = 50;
        Paint paint = new Paint();
        //paint.setStyle(Paint.Style.FILL);
        paint.setTextSize(x / 2);

         ...

    }
}

唯一的问题是我用来序列化对象的方法只适用于简单对象(即可以压缩为键值对的对象) 下面是序列化和读取对象方法:

 //Serialize object 
 public static byte[] getSerializedObject(Serializable s) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(baos);
            oos.writeObject(s);
        } catch (IOException e) {

            return null;
        } finally {
            try {
                oos.close();
            } catch (IOException e) {}
        }
        byte[] result = baos.toByteArray();

        return result;
    }

    //read object 
    public static Object readSerializedObject(byte[] in) {
        Object result = null;
        ByteArrayInputStream bais = new ByteArrayInputStream(in);
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(bais);
            result = ois.readObject();
        } catch (Exception e) {
            result = null;
        } finally {
            try {
                ois.close();
            } catch (Throwable e) {
            }
        }
        return result;
    } 

似乎我可以将对象转换为字节数组,但每当我读回对象时它返回null

1 个答案:

答案 0 :(得分:0)

您的代码适用于我:

import java.io.*;
class Foo implements Serializable {
    @Override public String toString() {
        return "Foo [x="+x+"]";
    }

    int x;
}
class Bar implements Serializable {
    @Override public String toString() {
        return "Bar [foo="+foo+"]";
    }

    Foo foo;
}
public class Soxx {
    public static byte[] getSerializedObject(Serializable s) {
        ByteArrayOutputStream baos=new ByteArrayOutputStream();
        ObjectOutputStream oos=null;
        try {
            oos=new ObjectOutputStream(baos);
            oos.writeObject(s);
        } catch(IOException e) {
            return null;
        } finally {
            try {
                oos.close();
            } catch(IOException e) {}
        }
        byte[] result=baos.toByteArray();
        return result;
    }
    //read object 
    public static Object readSerializedObject(byte[] in) {
        Object result=null;
        ByteArrayInputStream bais=new ByteArrayInputStream(in);
        ObjectInputStream ois=null;
        try {
            ois=new ObjectInputStream(bais);
            result=ois.readObject();
        } catch(Exception e) {
            result=null;
        } finally {
            try {
                ois.close();
            } catch(Throwable e) {}
        }
        return result;
    }
    public static void main(String[] args) {
       Foo foo=new Foo();
       foo.x=42;
       Bar bar=new Bar();
       bar.foo=foo;
       byte[] bytes=getSerializedObject(bar);
       Object object=readSerializedObject(bytes);
       System.out.println(object);
    }
}

输出:Bar [foo = Foo [x = 42]]