android:使用意图将 Parcelable 对象从服务解析为活动时出现问题

发布于 2024-12-09 04:42:27 字数 7897 浏览 0 评论 0原文

我尝试使用 android Intent 将 Order (请参阅下面的 Order 对象详细信息)对象从 Android 服务 (IntentService) 传递到活动,我尝试了以下方式:

Service 类中:

Bundle b = new Bundle();
b.putParcelable("lastOrder", serverResponse.getOrder());

Activity 中:

Bundle resultData
Order lastOrder = (Order) resultData.getParcelable("lastOrder");

但出现以下错误:

  1. “解组时出现 ClassNotFound 异常”,所以我参考以下链接修复了问题 http://androidideasblog.blogspot.com/2010/08/classnotfound-exception -when.html
  2. 现在我收到“readBundle:坏幻数”
  3. 正在解组未知类型代码

有人可以帮助我吗?

    public class Order implements Parcelable {
       private Long order_id;
       private String order_source;
       private ArrayList<OrderItem> items;
       private Shop shop;
        .....

         @Override
        public void writeToParcel(Parcel out, int flags) {
         out.writeLong(order_id);
         Bundle b = new Bundle();
            b.putParcelableArrayList("OrderItems", items);
            out.writeBundle(b);

           // out.writeList(items);
            out.writeParcelable(shop, 1);
        }

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

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

        private Order(Parcel in) {
           this.order_id = in.readLong();
             Bundle b = in.readBundle(OrderItem.class.getClassLoader());        
            this.items = b.getParcelableArrayList("OrderItems");

            //this.items = in.readArrayList(getClass().getClassLoader());
            this.shop = in.readParcelable(Shop.class.getClassLoader());
        }
    }

 // // // // // // // // // // // // // // // // // // // // // // // // //
   public class Shop implements Parcelable {

        private Long shop_id;
        private String shop_date;
        private String shop_code;   
        .....
         @Override
        public void writeToParcel(Parcel out, int flags) {
            out.writeLong(shop_id);
            out.writeString(shop_date);
            out.writeString(shop_code);
        }

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

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

        private Shop(Parcel in) {
            this.shop_id = in.readLong();
            this.shop_date = in.readString();
            this.shop_code = in.readString();
            }
    }

// // // // // // // // // // // // // // // // // // // // // // // // //
    public class OrderItem implements Parcelable {
        private Long order_item_id;
        private Product product;
        private ProductCategory product_Category;
        private ArrayList<OrderExtra> extras;

        .....
         @Override
        public void writeToParcel(Parcel out, int flags) {
            out.writeLong(order_item_id);
            out.writeParcelable(product,1 );
            out.writeParcelable(product_Category, 1);
            //out.writeList(extras); http://androidideasblog.blogspot.com/2010/08/classnotfound-exception-when.html
             Bundle b = new Bundle();
            b.putParcelableArrayList("OrderExtras", extras);
            out.writeBundle(b);
        }

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

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

        private OrderItem(Parcel in) {
            this.order_item_id = in.readLong();
            this.product = in.readParcelable(getClass().getClassLoader());
            this.product_Category = in.readParcelable(getClass().getClassLoader());
            //this.extras = in.readArrayList(getClass().getClassLoader());

            Bundle b = in.readBundle(OrderExtra.class.getClassLoader());        
            this.extras = b.getParcelableArrayList("OrderExtras");

        }
    }
    // // // // // // // // // // // // // // // // // // // // // // // // //
    public class Product implements Parcelable {
        private Long product_id;
        private String product_date;
        private String product_name;

        .....
         @Override
        public void writeToParcel(Parcel out, int flags) {
        out.writeLong(product_id);
            out.writeString(product_date);
            out.writeString(product_name);
        }

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

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

        private Product(Parcel in) {
            this.product_id = in.readLong();
            this.product_date = in.readString();
            this.product_name = in.readString();
        }
    }

// // // // // // // // // // // // // // // // // // // // // // // // //
    public class ProductCategory implements Parcelable {
        private Long product_category_id;
        private String product_category_date;
        private String product_category_name;

            .....
             @Override
        public void writeToParcel(Parcel out, int flags) {
           out.writeLong(product_category_id);
            out.writeString(product_category_date);
            out.writeString(product_category_name);

        }

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

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

        private ProductCategory(Parcel in) {
            this.product_category_id = in.readLong();
            this.product_category_date = in.readString();
            this.product_category_name = in.readString();
        }
    }

    // // // // // // // // // // // // // // // // // // // // // // // // //
    public class OrderExtra implements Parcelable {
        private Long order_item_extra_id;
        private String order_item_extra_price;
        private String order_item_extra_name;

         @Override
        public void writeToParcel(Parcel out, int flags) {
         out.writeLong(order_item_extra_id);
           out.writeString(order_item_extra_price);
            out.writeString(order_item_extra_name);
        }


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

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

        private OrderExtra(Parcel in) {
            this.order_item_extra_id = in.readLong();
            this.order_item_extra_price= in.readString();
            this.order_item_extra_name= in.readString();
        }
    }

I tried to pass the Order (see below for Order object details) object from Android Service (IntentService) to activity using android Intent, I tried in the following way:

in the Service class:

Bundle b = new Bundle();
b.putParcelable("lastOrder", serverResponse.getOrder());

In the Activity:

Bundle resultData
Order lastOrder = (Order) resultData.getParcelable("lastOrder");

but I got the following errors:

  1. “ClassNotFound exception when unmarshalling” so I fixed the issue refering to following link
    http://androidideasblog.blogspot.com/2010/08/classnotfound-exception-when.html
  2. now I'm getting “readBundle: bad magic number”
  3. Unmarshalling unknown type code

Can some help me on this?

    public class Order implements Parcelable {
       private Long order_id;
       private String order_source;
       private ArrayList<OrderItem> items;
       private Shop shop;
        .....

         @Override
        public void writeToParcel(Parcel out, int flags) {
         out.writeLong(order_id);
         Bundle b = new Bundle();
            b.putParcelableArrayList("OrderItems", items);
            out.writeBundle(b);

           // out.writeList(items);
            out.writeParcelable(shop, 1);
        }

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

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

        private Order(Parcel in) {
           this.order_id = in.readLong();
             Bundle b = in.readBundle(OrderItem.class.getClassLoader());        
            this.items = b.getParcelableArrayList("OrderItems");

            //this.items = in.readArrayList(getClass().getClassLoader());
            this.shop = in.readParcelable(Shop.class.getClassLoader());
        }
    }

 // // // // // // // // // // // // // // // // // // // // // // // // //
   public class Shop implements Parcelable {

        private Long shop_id;
        private String shop_date;
        private String shop_code;   
        .....
         @Override
        public void writeToParcel(Parcel out, int flags) {
            out.writeLong(shop_id);
            out.writeString(shop_date);
            out.writeString(shop_code);
        }

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

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

        private Shop(Parcel in) {
            this.shop_id = in.readLong();
            this.shop_date = in.readString();
            this.shop_code = in.readString();
            }
    }

// // // // // // // // // // // // // // // // // // // // // // // // //
    public class OrderItem implements Parcelable {
        private Long order_item_id;
        private Product product;
        private ProductCategory product_Category;
        private ArrayList<OrderExtra> extras;

        .....
         @Override
        public void writeToParcel(Parcel out, int flags) {
            out.writeLong(order_item_id);
            out.writeParcelable(product,1 );
            out.writeParcelable(product_Category, 1);
            //out.writeList(extras); http://androidideasblog.blogspot.com/2010/08/classnotfound-exception-when.html
             Bundle b = new Bundle();
            b.putParcelableArrayList("OrderExtras", extras);
            out.writeBundle(b);
        }

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

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

        private OrderItem(Parcel in) {
            this.order_item_id = in.readLong();
            this.product = in.readParcelable(getClass().getClassLoader());
            this.product_Category = in.readParcelable(getClass().getClassLoader());
            //this.extras = in.readArrayList(getClass().getClassLoader());

            Bundle b = in.readBundle(OrderExtra.class.getClassLoader());        
            this.extras = b.getParcelableArrayList("OrderExtras");

        }
    }
    // // // // // // // // // // // // // // // // // // // // // // // // //
    public class Product implements Parcelable {
        private Long product_id;
        private String product_date;
        private String product_name;

        .....
         @Override
        public void writeToParcel(Parcel out, int flags) {
        out.writeLong(product_id);
            out.writeString(product_date);
            out.writeString(product_name);
        }

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

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

        private Product(Parcel in) {
            this.product_id = in.readLong();
            this.product_date = in.readString();
            this.product_name = in.readString();
        }
    }

// // // // // // // // // // // // // // // // // // // // // // // // //
    public class ProductCategory implements Parcelable {
        private Long product_category_id;
        private String product_category_date;
        private String product_category_name;

            .....
             @Override
        public void writeToParcel(Parcel out, int flags) {
           out.writeLong(product_category_id);
            out.writeString(product_category_date);
            out.writeString(product_category_name);

        }

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

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

        private ProductCategory(Parcel in) {
            this.product_category_id = in.readLong();
            this.product_category_date = in.readString();
            this.product_category_name = in.readString();
        }
    }

    // // // // // // // // // // // // // // // // // // // // // // // // //
    public class OrderExtra implements Parcelable {
        private Long order_item_extra_id;
        private String order_item_extra_price;
        private String order_item_extra_name;

         @Override
        public void writeToParcel(Parcel out, int flags) {
         out.writeLong(order_item_extra_id);
           out.writeString(order_item_extra_price);
            out.writeString(order_item_extra_name);
        }


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

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

        private OrderExtra(Parcel in) {
            this.order_item_extra_id = in.readLong();
            this.order_item_extra_price= in.readString();
            this.order_item_extra_name= in.readString();
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文