2012-09-13 91 views
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]; 
    } 
}; 

}

回答

0

您可能需要居然把東西在createFromParcel和writeToParcel(如對象的內容)

2

這是正確的方式使用包裹轉移您的物品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()); 
    } 
} 

logcat的輸出

09-13 21:57:20.578: DEBUG/MyTag(1478): cost : 10 name: hello total channels: 123 list count: 0 
+0

想你的代碼,並仍然得到錯誤: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 ... –

+0

@ user461844確保您的對象中沒有字段爲空。我試過上面的代碼並且工作正常。如果你繼續得到錯誤給出錯誤行 –

+0

我已經修改了代碼,如下所示: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。 –

相關問題