Pass a parcelable object containing a map between activity

My ExpenseFB class that implements Parcelable contains a Map of UserFB (which implements Parcelable ):

ExpenseFB:

 public class ExpenseFB implements Parcelable { private String id; private String name; private String description; private String whopaidID; private String whopaidName; private Double amount; private Map<String, UserFB> partecipants; // setters and getters... @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(id); dest.writeString(name); dest.writeString(description); dest.writeString(whopaidID); dest.writeString(whopaidName); dest.writeMap(partecipants); } protected ExpenseFB(Parcel in) { id = in.readString(); name = in.readString(); description = in.readString(); whopaidID = in.readString(); whopaidName = in.readString(); in.readMap(partecipants,UserFB.class.getClassLoader()); } public static final Creator<ExpenseFB> CREATOR = new Creator<ExpenseFB>() { @Override public ExpenseFB createFromParcel(Parcel in) { return new ExpenseFB(in); } @Override public ExpenseFB[] newArray(int size) { return new ExpenseFB[size]; } }; } 

UserFB:

 public class UserFB implements Parcelable{ private String id; private String name; private String email; private Map<String, GroupFB> groups; private Map<String, UserFB> friends; // setters and getters @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(id); dest.writeString(name); dest.writeString(email); dest.writeMap(groups); dest.writeMap(friends); } protected UserFB(Parcel in) { id = in.readString(); name = in.readString(); email = in.readString(); in.readMap(groups,GroupFB.class.getClassLoader()); in.readMap(friends,UserFB.class.getClassLoader()); } public static final Creator<UserFB> CREATOR = new Creator<UserFB>() { @Override public UserFB createFromParcel(Parcel in) { return new UserFB(in); } @Override public UserFB[] newArray(int size) { return new UserFB[size]; } }; } 

I want to pass an ExpenseFB object between two actions by adding an ExpenseFB object to the target:

 intent.putExtra("id", expenseFB); 

When I run getIntent().getParcelableExtra("id") in debug mode in the second step, it throws the following exception when trying to execute the readMap() method on the partecipants map:

  ... Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.Map.put(java.lang.Object, java.lang.Object)' on a null object reference 

I see that the partecipants card in the first step is full: I think the problem is with the writeMap () method.
Is there a standard or better way to pass a Parcelable object containing a map?
Should I ask for another method to send the Card?
I do not want to use the Serializable object, because I read that they make worse.

+5
source share
2 answers

The problem is that readMap() used to read data from Parcel into and existing Map . You did not create a Map before calling readMap() , so you will get a NullPointerException .

You can solve this problem by initializing the map when you declare it:

 private Map<String, GroupFB> groups = new HashMap<String, GroupFB>(); private Map<String, UserFB> friends = new HashMap<String, UserFB>(); 

Or you can create an empty Map in the UserFB constructor, for example:

 protected UserFB(Parcel in) { id = in.readString(); name = in.readString(); email = in.readString(); groups = new HashMap<String, GroupFB>(); in.readMap(groups,GroupFB.class.getClassLoader()); friends = new HashMap<String, UserFB>() in.readMap(friends,UserFB.class.getClassLoader()); } 
+4
source

You have a point, but I think you need to know how to write Map<> in parcelable

Insert writeParcel() method

 @Override public void writeToParcel(Parcel dest, int flags) { dest.writeInt(this.groups.size()); for (Map.Entry<String, GroupFb> entry : this.groups.entrySet()) { dest.writeString(entry.getKey()); dest.writeParcelable(entry.getValue(), flags); } } protected UserFB (Parcel in) { int groupsSize = in.readInt(); this.groups = new HashMap<String, GroupFb>(groupsSize); for (int i = 0; i < groupsSize; i++) { String key = in.readString(); GroupFb value = in.readParcelable(GroupFb.class.getClassLoader()); this.groups.put(key, value); } } 

Do the same for the other Map<> .

0
source

Source: https://habr.com/ru/post/1266850/


All Articles