实施ExpenseFB
的我的班级Parcelable
包含Map
UserFB
(也实施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];
}
};
}
我想通过添加对象在两个活动之间传递ExpenseFB对象
ExpenseFB
意图:
intent.putExtra("id", expenseFB);
在调试模式下,当我在第二个活动中执行getIntent().getParcelableExtra("id")
时,当尝试在readMap()
地图上执行partecipants
方法时,会引发以下异常:
... 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
我看到第一个活动中的partecipants
地图已填满:我认为问题出在 writeMap()方法中。
是否存在传递包含地图的Parcelable
对象的标准或更好方法?
我是否要调用另一种方法来包裹地图?
我不想使用Serializable
对象,因为我读到他们的表现更差。
答案 0 :(得分:6)
问题在于readMap()
用于将数据从Parcel
读取到和现有Map
。在致电Map
之前,您尚未创建readMap()
,因此您获得了NullPointerException
。
您可以通过在声明地图时初始化地图来解决此问题:
private Map<String, GroupFB> groups = new HashMap<String, GroupFB>();
private Map<String, UserFB> friends = new HashMap<String, UserFB>();
或者,您可以在Map
构造函数中创建空UserFB
,如下所示:
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());
}
答案 1 :(得分:1)
你明白了,但我认为你需要知道如何将Map<>
写入parcelable
粘贴writeParcel()
方法
@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);
}
}
对另一个Map<>
也这样做。