3

I have a complex object (which includes further objects and hashmaps). All objects implement Serializable.

In the first activity I do:

public void secondActivity(MyObject o) {
    Intent intent = new Intent(this, SecondActivity.class);
    intent.putExtra("myobject", o);

    startActivity(intent);
    finish();
} 

Then, in the second activity I'm doing:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = getIntent();
    MyObject o = (MyObject)intent.getSerializableExtra("myobject");
}

But when casting I'm getting this exception:

java.lang.ClassCastException: java.util.ArrayList cannot be cast to myapp.MyObject

Is this the correct way to pass complex objects between activities? What am I missing?

Or should I implement singleton class?

Ivan-Mark Debono
  • 15,500
  • 29
  • 132
  • 263

2 Answers2

2

You are doing all fine, so I think that can be:

  • your are sending to secondActivity method an incorrect object (like ArrayList instead of correct MyObject)
  • Please look if you are using this key "myobject" in other part of the code and setting other object in putExtra.

  • verify MyObject class and look if some variable is an ArrayList and remove it (just to test)

  • your custom object not have "implements Serializable" like:

    public class MyObject implements Serializable {

    //variables and methods

    }

So, I suggest that you verify this, and I think is better use Singleton class or Parcelable instead of Serializable, because Google says that is better acording to Good Practice. Please look this answer stackoverflow

I make some app and implement serializable, my custom object is:

public class Stores implements Serializable {
private static final long serialVersionUID = 1L;
public Integer id;
public String name;
public String store_hours;
public String telephone;
public String fax;
public String latitude;
public String longitude;

}

My firt Activity:

private void showDialog(Stores store)
    {
        Intent intent = new Intent(this,DisplayDialogActivity.class);
        intent.putExtra("Locations",store);
        startActivityForResult(intent, 0);
    }

And my second activity

Stores store = (Stores) getIntent().getSerializableExtra("Locations");

PD: I don't know speak english very well, so apologize for this.

Community
  • 1
  • 1
Celggar
  • 128
  • 1
  • 6
0
  • There are some general guidelines that why some objects are not serializable

  • It is too closely tied to native code (java.util.zip.Deflater).

  • The object's state depends on the internals of the virtual machine or the runtime environment and thus may change from run to run
    (java.lang.Thread, java.io.InputStream, java.io.FileDescriptor,
    java.awt.PrintJob).

  • The class is mostly a holder for static methods without any real internal state (java.beans.Beans, java.lang.Math).

  • The class is a nonstatic inner class. Serialization just doesn't work well with nonstatic inner classes. (Static inner classes have no problem being serialized.)

  • An alternate serialization format is preferred in a particular context. (XOM node classes are not serializable because the proper serialization format for XML is XML.)

Make sure your class is declared public

complete Reference . Java I/O 2nd Edition