0

我正在使用 parcelable 将一个对象从一个活动转移到另一个活动,但它显示 null 作为在另一个活动中检索该对象。有什么建议么?

这是代码:

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];
    }
};

}

4

2 回答 2

2

这是使用 parcel andorid 传输对象的正确方法

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 TPackage(int cost, String name, int totalChannels,
        HashMap<String, Channel> channelList){
    this.cost = cost;
    this.name = name;
    this.totalChannels = totalChannels;
    this.channelList = channelList;
}


public TPackage(Parcel in){
    //same order as that in writeToParcel()
    cost = in.readInt();
    name = in.readString();
    totalChannels = in.readInt();
    Bundle bundle = in.readBundle();
    channelList = (HashMap<String, Channel>) bundle.getSerializable("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) {
    dest.writeInt(cost);
    dest.writeString(name);
    dest.writeInt(totalChannels);
    //easiest way to transfer HashMap via parcel
    Bundle bundle = new Bundle();
    bundle.putSerializable("channelList", channelList);
    dest.writeBundle(bundle);
}

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

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

}

[已编辑] 这是发件人活动

public class MyActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TPackage tPackage = new TPackage(10, "hello", 123, new HashMap<String, Channel>());
        Intent intent = new Intent(this, NActivity.class);
        intent.putExtra("parcel", tPackage);
        startActivity(intent);
    }
}

这是接收器活动

public class NActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        TPackage tPackage = intent.getParcelableExtra("parcel");
        HashMap<String, Channel> hashMap = tPackage.getList();
        Log.d("MyTag", "cost : " + tPackage.getCost() + " name: " + tPackage.getName()
                + " total channels: " + tPackage.getTotal_channels() + " list count: " + hashMap.size());
    }
}

日志输出

09-13 21:57:20.578: DEBUG/MyTag(1478): cost : 10 name: hello total channels: 123 list count: 0
于 2012-09-13T13:38:43.323 回答
0

您可能需要在 createFromParcel 和 writeToParcel 中实际放置一些内容(例如对象的内容)

于 2012-09-13T13:24:59.207 回答