Android:将对象传递给实现Parcelable的Activity时出错

时间:2015-02-27 00:39:14

标签: android

我正在将一个对象传递给一个实现Parceable的Activity,但是当我尝试获取它时,我得到了错误。我正在关注这个Android Parcelable object return Null,但我看不出错误在哪里。你能帮我吗?

Ver_Activity.java

(...)
ArrayList<Grafico> graficos;
(...)
intent = new Intent(VerActivity.this, Ver_GraficosActivity.class);
intent.putParcelableArrayListExtra("graficos", graficos);
startActivity(intent);

Ver_Graficos.java

public class Ver_GraficosActivity extends ActionBarActivity{

    private static int[] COLORS = new int[] { Color.GREEN, Color.BLUE,Color.MAGENTA, Color.CYAN };
    DatabaseHelper databaseHelper;
    private CategorySeries mSeries = new CategorySeries("");
    private DefaultRenderer mRenderer = new DefaultRenderer();
    private GraphicalView mChartView;
    private double []values=new double []{};
    private String []legenda=new String[]{};
    //ArrayList<Grafico> graficos = new ArrayList<Grafico>(){};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ver__graficos);

  ArrayList<Grafico> graficos=getIntent().getParcelableArrayListExtra("graficos");

Grafico.class

public class Grafico implements Parcelable {

    private int despesa;
    private String categoria;
    private double percentagem;

    public Grafico(){}

    public Grafico(int despesa, String categoria, double percentagem ) {
        this.despesa = despesa;
        this.categoria = categoria;
        this.percentagem = percentagem;
    }

    public Grafico(int despesa, String categoria) {
        this.despesa = despesa;
        this.categoria = categoria;
    }

    private Grafico(Parcel in) {
        // This order must match the order in writeToParcel()
        this.categoria = in.readString();
        this.despesa = in.readInt();
        this.percentagem = in.readDouble();
        // Continue doing this for the rest of your member data
    }

    public int getDespesa() {
        return despesa;
    }

    public void setDespesa(int despesa) {
        this.despesa = despesa;
    }

    public double getPercentagem() {
        return percentagem;
    }

    public void setPercentagem(double percentagem) {
        this.percentagem = percentagem;
    }

    public String getCategoria() {
        return categoria;
    }

    public void setCategoria(String categoria) {
        this.categoria = categoria;
    }

    // Just cut and paste this for now
    public int describeContents() {
        return 0;
    }

    public void writeToParcel(Parcel out, int flags) {
        out.writeInt(this.despesa);
        out.writeString(this.categoria);
        out.writeDouble(this.percentagem);
    }

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

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

错误:

FATAL EXCEPTION: main Process: com.android., PID: 8317
        java.lang.RuntimeException: Unable to start activity ComponentInfo{.Ver_GraficosActivity}: java.lang.RuntimeException: Parcel android.os.Parcel@26d3f280: Unmarshalling unknown type code 7340147 at offset 320
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
                at android.app.ActivityThread.access$800(ActivityThread.java:144)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:135)
                at android.app.ActivityThread.main(ActivityThread.java:5221)
                at java.lang.reflect.Method.invoke(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:372)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
         Caused by: java.lang.RuntimeException: Parcel android.os.Parcel@26d3f280: Unmarshalling unknown type code 7340147 at offset 320
                at android.os.Parcel.readValue(Parcel.java:2222)
                at android.os.Parcel.readListInternal(Parcel.java:2520)
                at android.os.Parcel.readArrayList(Parcel.java:1836)
                at android.os.Parcel.readValue(Parcel.java:2167)

1 个答案:

答案 0 :(得分:0)

尝试完所有内容后,我注意到属性的顺序不正确: 此

 private Grafico(Parcel in) {
    this.categoria = in.readString();
    this.despesa = in.readInt();
    this.percentagem = in.readDouble();
}

应该是(维护构造函数的顺序):

private Grafico(Parcel in) {
        this.despesa = in.readInt();
        this.categoria = in.readString();
        this.percentagem = in.readDouble();
    }

这解决了我的问题。