0
我使用parcelable將一個對象從一個活動轉移到另一個活動,但它顯示爲空 在另一活動中檢索該對象。 有什麼建議嗎?如何使用parcelable將對象從一項活動轉移到另一項活動?
下面的代碼:
public class TPackage implements Parcelable {
// private static final long serialVersionUID = 1L;
private int cost;
private String name;
private int totalChannels;
private HashMap<String, Channel> channelList;
public int getCost() {
return cost;
}
public void setCost(int cost) {
this.cost = cost;
}
public int getTotal_channels() {
return totalChannels;
}
public void setTotal_channels(int total_channels) {
this.totalChannels = total_channels;
}
public HashMap<String, Channel> getList() {
return channelList;
}
public void setList(HashMap<String, Channel> list) {
this.channelList = list;
}
public void put(String name, Channel c) {
channelList.put(name, c);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel dest, int flags) {
}
public static final Parcelable.Creator<TPackage> CREATOR = new Parcelable.Creator<TPackage>() {
public TPackage createFromParcel(Parcel in) {
return new TPackage();
}
public TPackage[] newArray(int size) {
return new TPackage[size];
}
};
}
想你的代碼,並仍然得到錯誤:09-13 19:24:15.507: E/AndroidRuntime(3674):致命例外:main 09-13 19:24:15.507:E/AndroidRuntime(3674):java.lang.RuntimeException:包裹:無法封送值[email protected] 09-13 19:24:15.507:E/AndroidRuntime(3674):\t at android.os.Parcel.writeValue(Parcel.java:1137) 09-13 19:24:15.507:E/AndroidRuntime(3674): \t at android.os.Parcel.writeMapInternal(Parcel.java:493) 09-13 19:24:15.507:E/AndroidRuntime(3674):\t at android.os.Parcel.writeMap(Parcel.java:477) 09-13 19:24:15.507:E/AndroidRuntime(3674):\t at ... –
@ user461844確保您的對象中沒有字段爲空。我試過上面的代碼並且工作正常。如果你繼續得到錯誤給出錯誤行 –
我已經修改了代碼,如下所示:public void writeToParcel(Parcel dest,int flags){ \t dest.writeInt(12); \t dest.writeString(「as」); \t dest.writeInt(2); \t //通過包裹傳輸HashMap的最簡單方法 \t Bundle bundle = new Bundle(); \t bundle.putSerializable(「channelList」,new HashMap()); \t dest.writeBundle(bundle); \t} ...讀取時獲取對象爲null。 –