简体   繁体   中英

How to send a serializable object with nonserializable children from one Android activity to another using intent

I have a seralisable class with nonserializable child. I want to send the object of this class from ona android activity to another, but I cannot. The classes are following:

public class A implements Serializable{

/**
 * generated serial version UID
 */
private static final long serialVersionUID = -3445290513860716092L;

private B b;

public A () {}

public B getB() {
    return b;
}

public void setB(B b) {
    this.b = b;
}
}

Where the class B is not serializable

public class B{

private int ID;
protected String name;

public B() {}

...
}

I use putExtra method of Intent to send object.

Intent i = new Intent(ActivityA.this, ActivityB.class);
A a = new A();
B b = new B();
b.setID(1);
b.setName("Name");
a.setB(b);
i.putExtra("object", a);
startActivity(i);

An error occurs when activity starts. What is wrong?

To serialize a class, all its children MUST be serializable. You cannot do it with non-serializable children. You will have to make all its children (and their children too) serializable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM