0

我有一个包含 MenuItem 对象的 ArrayList 的订单对象。这两个类都实现了 Parcelable 接口。

当我尝试从新活动中的 Intent 检索对象时,出现以下错误:

java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.waitron5/com.example.waitron5.SectionsActivity}:android.os.BadParcelableException:解组时 ClassNotFoundException:com.example.waitron5.MenuItem

这是我发送 Order 对象的代码:

i = new Intent(v.getContext(), SectionsActivity.class);
            i.putExtra("data", order);
            startActivity(i);

这是我检索 Order 对象的母鹿:

Intent i = getIntent();
    if(i != null && i.hasExtra("data"))
    {
        order = i.getParcelableExtra("data");
        Toast.makeText(
            this.getApplicationContext(), 
            "recieved order object, covers: " + 
                order.getCovers() + 
                " table no: " + 
                order.getTable() + 
                " order size: " + 
                order.getItems().size(), 
            Toast.LENGTH_SHORT).show();

订单类别:

package com.example.waitron5;

import java.util.ArrayList;
import android.os.Parcel;
import android.os.Parcelable;

public class Order implements Parcelable {

    private int covers;
    private int table;
    private ArrayList<MenuItem> items;


    public Order(){
        super();
        this.items = new ArrayList<MenuItem>();

    }

    public static final Parcelable.Creator<Order> CREATOR = 
    new Parcelable.Creator<Order>() {

        @Override
        public Order createFromParcel(Parcel source) {
            return new Order(source);
        }

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


    };

    public Order(int covers, int table) {
        super();
        this.covers = covers;
        this.table = table;
        this.items = new ArrayList<MenuItem>();
    }

    public int getCovers() {
        return covers;
    }

    public void setCovers(int covers) {
        this.covers = covers;
    }

    public int getTable() {
        return table;
    }

    public void setTable(int table) {
        this.table = table;
    }

    public ArrayList<MenuItem> getItems() {
        return items;
    }

    public void setItems(ArrayList<MenuItem> items) {
        this.items = items;
    }

    //add to order
    public void addToOrder(MenuItem m){
        items.add(m);
    }

    //remove from order
    public void removeFromOrder(MenuItem m){
        items.remove(m);
    }

    public Order(Parcel source) {
        this();
        readFromParcel(source);
    }

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        // TODO Auto-generated method stub
        dest.writeInt(covers);
        dest.writeInt(table);
        dest.writeList(items);
    }

    @SuppressWarnings("unchecked")
    private void readFromParcel(Parcel source) {
        // TODO Auto-generated method stub
        covers = source.readInt();
        table = source.readInt();
        source.readList(items, null);

    }
}

菜单项类:

package com.example.waitron5;

import android.os.Parcel;
import android.os.Parcelable;

public class MenuItem implements Parcelable {

    private String name;
    private double price;
    private int qty;
    private String info;

    public MenuItem(String name, double price) {
        super();
        this.name = name;
        this.price = price;
    }
    public MenuItem() {
        // TODO Auto-generated constructor stub
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }

    public int getQty() {
        return qty;
    }
    public void setQty(int qty) {
        this.qty = qty;
    }
    public String getInfo() {
        return info;
    }
    public void setInfo(String info) {
        this.info = info;
    }
    @Override
    public String toString() {
        return name;
    }


    //Parcelable methods.
    public static final Parcelable.Creator<MenuItem> CREATOR = 
        new Parcelable.Creator<MenuItem>() {

        @Override
        public MenuItem createFromParcel(Parcel source) {
            return new MenuItem(source);
        }

        @Override
        public MenuItem[] newArray(int size) {
            return new MenuItem[size];
        }
    };

    public MenuItem(Parcel source) {
        this();
        readFromParcel(source);
    }

    private void readFromParcel(Parcel source) {
        name = source.readString();
        price = source.readDouble();
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeDouble(price);
    }
}

关于可能发生的事情有什么想法吗?

4

1 回答 1

2

我解决了我的问题。问题如下:

在订单类中。

public void writeToParcel(Parcel dest, int flags) {

    dest.writeInt(covers);

    dest.writeInt(table);

    dest.writeList(items);

}

应该:

public void writeToParcel(Parcel dest, int flags) {

    dest.writeInt(covers);

    dest.writeInt(table);

    dest.writeTypedList(items);

}

private void readFromParcel(Parcel source) {

    covers = source.readInt();

    table = source.readInt();

    source.readList(items, null);

}

应该:

private  void readFromParcel(Parcel source) {

    covers = source.readInt();

    table = source.readInt();

    items = new ArrayList<MenuItem>();

    source.readTypedList(items,  MenuItem.CREATOR);

}
于 2013-01-10T02:28:08.777 回答