我正在尝试实现一个活动,该活动获取对象列表并传递给另一个活动进行处理。但我得到了NPE。这是我的代码。
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) {
this.customerId = customerId;
products = new HashMap<String, OrderProduct>();
}
public Order(Parcel in)
{
product.productId = in.readString();
product.qty = in.readString();
}
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
dest.writeString(product.getProductId());
dest.writeString(product.qty);
}
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];
}
};
}
############################# ACTIVITY 1 ##################
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.send_menu:
//saveOrder(order);
showOrderReview();
break;
}
return true;
}
############## ACITIVITY 2
public class ReviewOrderActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
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());
}
}
答案 0 :(得分:1)
如果没有看到您的LogCat,这是最突出的:
public Order(Parcel in) {
product.productId = in.readString();
product.qty = in.readString();
}
调用此构造函数时,product
为null。此外,在此构造函数中,未定义products
。相反,做:
public Order(Parcel in) {
String productId = in.readString();
String qty = in.readString();
product = new OrderProduct(productId, qty);
products = new HashMap<String, OrderProduct>();
}