我有2个活动(Order和OrderReview),我将OrderActivity中的对象发送到OrderReviewActivity。当我在OrderReview活动中检索它时,该对象为NULL。
订单活动
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.send_menu:
showOrderReview();
break;
}
return true;
}
private void showOrderReview()
{
// TODO Auto-generated method stub
Intent i = new Intent(this, ReviewOrderActivity.class);
i.putExtra("order",order);
Collection<Order.OrderProduct> products = order.getProducts();
System.out.println("OrderActivity : " + products.size());
for (Order.OrderProduct product : products){
Log.d("---", "OrderActivity product.getQty() "+product.getQty()+" "+product.getProductId());
}
startActivity(i);
}
OrderReviewActivity
public class ReviewOrderActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.orderreview);
Intent i = getIntent();
Order myOrderObject = (Order) i.getParcelableExtra("order");
Collection<Order.OrderProduct> products = myOrderObject.getProducts();
System.out.println("Orderrrrrrrrrrrr : " + products.size());
for (Order.OrderProduct product : products){
Log.d("---", " product.getQty() "+product.getQty()+" "+product.getProductId());
}
}
}
Order.java
public class Order implements Parcelable {
public static class OrderProduct {
private String productId;
private String qty;
public OrderProduct(String productId, String qty) {
this.productId = productId;
this.qty = qty;
}
public String getProductId() {
return productId;
}
public String getQty() {
return qty;
}
public void setQty(String qty) {
this.qty = qty;
}
@Override
public String toString() {
return "productId: " + productId + " qty: " + qty;
}
}
private String customerId;
private Map<String, OrderProduct> products;
private OrderProduct product;
public Order(String customerId) {
Log.v("TAG", "Order constr 1");
this.customerId = customerId;
products = new HashMap<String, OrderProduct>();
}
public Order(Parcel in)
{
Log.v("TAG", "ParcelData(Parcel source): time to put back parcel data");
String productId = in.readString();
String qty = in.readString();
Log.v("TAG", productId +" qty: "+qty);
product = new OrderProduct(productId, qty);
products = new HashMap<String, OrderProduct>();
}
public void setProduct(String productId, String qty) {
product = products.get(productId);
if (product == null) {
product = new OrderProduct(productId, qty);
}
else {
product.setQty(qty);
}
products.put(productId, product);
}
public Collection<OrderProduct> getProducts() {
return products.values();
}
public String getCustomerId() {
return customerId;
}
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("Order: customerId: " + customerId + " { ");
for (OrderProduct product : products.values()) {
sb.append(" { ").append(product.toString()).append(" } ");
}
sb.append(" } ");
return sb.toString();
}
@Override
public int describeContents()
{
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags)
{
// TODO Auto-generated method stub
System.out.println("INSIDE product.getProductId()" +product.getProductId());
dest.writeString(product.getProductId());
System.out.println("INSIDE product.getQty()" +product.getQty());
dest.writeString(product.getQty());
}
public static final Order.Creator<Order> CREATOR = new Order.Creator<Order>() {
public Order createFromParcel(Parcel in) {
return new Order(in);
}
public Order[] newArray(int size) {
return new Order[size];
}
};
}
答案 0 :(得分:0)
使用i.getSerializableExtra(“order”)而不是使用i.getParcelableExtra(“order”)。希望它有效