开发者

Problems with Serializing an Object Java

I have a tablelayout which implements serializable which keeps tablerows. in the tablerows, it wil开发者_开发问答l contain an edittext and a checkbox, much like a checklist to be exact.

However when I try serializing the tablelayout (CheckList) it tells me the that the tablerows are not serizable. I have after which implemented it to be serializable too. But it then, tells me that the EditText and CheckBox are not serializable.

How can I solve this?

UPDATE: (Serializer.java)

public class Serializer {

public static byte[] serializeObject(Object o) { 
    ByteArrayOutputStream bos = new ByteArrayOutputStream(); 

    try { 
      ObjectOutput out = new ObjectOutputStream(bos); 
      out.writeObject(o); 
      out.close(); 

      // Get the bytes of the serialized object 
      byte[] buf = bos.toByteArray(); 

      return buf; 
    } catch(IOException ioe) { 
      Log.e("serializeObject", "error", ioe); 

      return null; 
    } 
}

  public static Object deserializeObject(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; 
    } 
  } 
}

This is how I serialized the checklist:

        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putSerializable("checklist", Serializer.serializeObject(checklist));
        intent.putExtra("checklist", bundle);
        setResult(Activity.RESULT_OK, intent);
        finish();

And here's how I tried to deserialize it:

    try {
                Bundle extras = data.getExtras();
                Bundle checklist_bundle = extras.getBundle("checklist");
                byte[] bytes =  (byte[]) checklist_bundle.getSerializable("checklist");
                CheckList checklist = (CheckList) Serializer.deserializeObject(bytes);

            } catch (Exception e) {
                e.printStackTrace();
            }


I would extend the class, make the extended class serializable , and use that instead.

Not sure what else I can add to make this more substantial, but I guess we can build on it over time? :)


try to mark those fields as transients or implement Externalizable.

Regards, Stephane


caveat that I have not done any Android programming at all, but I did check the Bundle interface definition for Android.

http://developer.android.com/reference/android/os/Bundle.html#putSerializable%28java.lang.String,%20java.io.Serializable%29

You seem to be calling the wrong stuff. first off:

  • no need to create your own serializing/deserializing methods, ditch those, let the JVM serialization engine do the work

  • bundle.putSerializable("checklist", Serializer.serializeObject(checklist)); should just be bundle.putSerializable("checklist", checklist); make sure CheckList implements Serializable

  • byte[] bytes = (byte[]) checklist_bundle.getSerializable("checklist") should be CheckList ck = (CheckList) checklist_bundle.getSerializable("checklist")

  • There was mention above that you have a reference to the "App" from CheckList? I'm not sure what an "App" is, but if it's unserializable, slap a "transient" modifier to its member variable definition in the CheckList class. Note though that during deserialization, you have to properly "fill" that field in. The deserializer won't do that for you automatically (this is where Externalizable can come in)


And what about using the Parcelable interface in Android?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜