Sending Nested Parcelable with Intent

Figen Güngör

I am trying to send a Parcelable object which also contains another Parcelable object with Intent. However, I am getting NullPointer Exception. Could you please tell me where I am doing wrong?

A.java

public class A  implements Parcelable {

    private ArrayList<B> var;

    public A()
    {
        this.var = new ArrayList<B>();  
    }

    public void addToB(B b)
    {
        var.add(b);
    }


    public ArrayList<B> getB() {
        return var;
    }

    public void setB(ArrayList<B> b) {
        this.var = b;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {

        dest.writeTypedList(this.var);

    }

    private A (Parcel in){

        in.readTypedList(this.var, B.CREATOR);
    }

    public static final Parcelable.Creator<A> CREATOR = new Parcelable.Creator<A>() {
        public A createFromParcel(Parcel in) {
            return new A(in);
        }

        public A[] newArray(int size) {
            return new A[size];
        }
    };

}

B.java

public class B implements Parcelable  {

    private String type;


    public B(String type)
    {
        this.type=type;
    }

    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel arg0, int arg1) {
        arg0.writeString(this.type);        
    }

    private B(Parcel in) {
        this.type=in.readString();
    }

    public static final Parcelable.Creator<B> CREATOR = new Parcelable.Creator<B>() {
        public B createFromParcel(Parcel in) {
            return new B(in);
        }

        public B[] newArray(int size) {
            return new B[size];
        }
    };

}

I send the A object with Intent like this:

Intent i = new Intent(Bla.this, Blabla.class);
            i.putExtra("info", objectA);
            startActivity(i);

I receive the parcelable like this:

Intent i = getIntent();
        ObjectA ci = (ObjectA)i.getParcelableExtra("info");
odiiil

You have to instanciate your ArrayList< B > in :

 private A (Parcel in){
    var = new ArrayList<B>();
    in.readTypedList(this.var, B.CREATOR);
}

Say if it works :)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Sending Parcelable in Intent shows white screen when startActivity called

From Dev

How to broadcast a parcelable object in an intent?

From Dev

Pass array in Intent using parcelable

From Dev

android: sending intent via intent

From Dev

Sending Intent using ADB

From Dev

Sending an intent to Snapchat

From Dev

Sending Data through Intent

From Dev

Intent sending null error

From Dev

sending Intent from a fragment

From Dev

Class not found when Unmarshalling Android Intent Parcelable

From Dev

How to retrieve `Parcelable` data from an intent?

From Dev

NotSerializable exception with intent.addExtra(parcelable Object)

From Dev

Error passing parcelable object using intent

From Dev

Can't Pass Parcelable via Intent

From Dev

android - unable to pass parcelable in Intent to another activity

From Dev

Sending array list of object between activities with Parcelable

From Dev

Sending data to a Fragment instead of an Activity with a Parcelable object

From Dev

Sending data to a Fragment instead of an Activity with a Parcelable object

From Dev

Sending intent with bundle using console

From Dev

sending a customized object to intent android

From Dev

Sending message through WhatsApp By intent

From Dev

android - sending image via intent

From Dev

PendingIntent sending the wrong intent / putExtra

From Dev

Sending picture with intent to TypeApp Mail

From Dev

Intent sent by Alarm PendingIntent has a null Parcelable in the Bundle

From Dev

Read and write an ArrayList of ArrayLists containing Parcelable Objects to pass in an Intent

From Dev

Parcelable custom object losing instance values when passed with intent

From Dev

Pass interface between activities in intent - interface fails to be Serializable or Parcelable

From Dev

Android Intent passing parcelable object vs passing Json string

Related Related

  1. 1

    Sending Parcelable in Intent shows white screen when startActivity called

  2. 2

    How to broadcast a parcelable object in an intent?

  3. 3

    Pass array in Intent using parcelable

  4. 4

    android: sending intent via intent

  5. 5

    Sending Intent using ADB

  6. 6

    Sending an intent to Snapchat

  7. 7

    Sending Data through Intent

  8. 8

    Intent sending null error

  9. 9

    sending Intent from a fragment

  10. 10

    Class not found when Unmarshalling Android Intent Parcelable

  11. 11

    How to retrieve `Parcelable` data from an intent?

  12. 12

    NotSerializable exception with intent.addExtra(parcelable Object)

  13. 13

    Error passing parcelable object using intent

  14. 14

    Can't Pass Parcelable via Intent

  15. 15

    android - unable to pass parcelable in Intent to another activity

  16. 16

    Sending array list of object between activities with Parcelable

  17. 17

    Sending data to a Fragment instead of an Activity with a Parcelable object

  18. 18

    Sending data to a Fragment instead of an Activity with a Parcelable object

  19. 19

    Sending intent with bundle using console

  20. 20

    sending a customized object to intent android

  21. 21

    Sending message through WhatsApp By intent

  22. 22

    android - sending image via intent

  23. 23

    PendingIntent sending the wrong intent / putExtra

  24. 24

    Sending picture with intent to TypeApp Mail

  25. 25

    Intent sent by Alarm PendingIntent has a null Parcelable in the Bundle

  26. 26

    Read and write an ArrayList of ArrayLists containing Parcelable Objects to pass in an Intent

  27. 27

    Parcelable custom object losing instance values when passed with intent

  28. 28

    Pass interface between activities in intent - interface fails to be Serializable or Parcelable

  29. 29

    Android Intent passing parcelable object vs passing Json string

HotTag

Archive