将对象发送到另一个活动时的类强制转换异常

时间:2013-12-22 10:28:14

标签: java android parcelable

我尝试使用Intent发送一个对象。

我有三个类,A类有一个B类列表,B类有一个C类列表,我希望将A类从一个活动发送到另一个活动 Parcelable。 我尝试下面的代码:

中的

private List<DocumentItem> documentItems;
private int DocNum ;

    public CycleAccount(Parcel in) {
    DocNum = in.readInt();
    documentItems = new ArrayList<DocumentItem>();
    in.readList(documentItems , null);
}

@Override
public int describeContents() {
    return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {

    dest.writeInt(DocNum);
    dest.writeList(documentItems);


}

public CycleAccount(Parcel in) {
    DocNum = in.readInt();
    documentItems = new ArrayList<DocumentItem>();
    in.readList(documentItems , null);
}
B级

private String LedgerAcountId;
private String  Title;
private String Debit;
private String Credit;
private String Description;
private List<DetailsAccount> DetailAccount;


public DocumentItem(Parcel in) {
    LedgerAcountId = in.readString();
    Title = in.readString();
    Debit = in.readString();
    Credit = in.readString();
    Description = in.readString();

    DetailAccount = new ArrayList<DetailsAccount>();
    in.readList(DetailAccount, null);


}

@Override
public int describeContents() {

    return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(LedgerAcountId);
    dest.writeString(Title);
    dest.writeString(Debit);
    dest.writeString(Credit);
    dest.writeString(Description);
    dest.writeList(DetailAccount);

}

并在课堂c:

private String Title;
private String Level;
private String detailAccountId;


DetailsAccount(Parcel in)
{
    Title = in.readString();
    Level = in.readString();
    detailAccountId = in.readString();
}

@Override
public int describeContents() {

    return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {

    dest.writeString(Title);
    dest.writeString(Level);
    dest.writeString(detailAccountId);
}

用于发送和获取数据我使用以下代码:

发送:

Intent intent = new Intent(AccountReviewActivity.this, CycleAccountAccountReview.class);
Bundle bundle = new Bundle();
bundle.putParcelable("data", CycleItems);
intent.putExtra("data",bundle);
startActivity(intent);

获得:

cycleItem = new CycleAccount();
Bundle bundle = getIntent().getExtras();
cycleItem = bundle.getParcelable("data");

我得到以下错误:

12-22 10:13:57.320: E/AndroidRuntime(3702): FATAL EXCEPTION: main
12-22 10:13:57.320: E/AndroidRuntime(3702): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.proj.finantial/activity.CycleAccountAccountReview}: java.lang.ClassCastException: android.os.Bundle cannot be cast to classes.CycleAccount
12-22 10:13:57.320: E/AndroidRuntime(3702):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-22 10:13:57.320: E/AndroidRuntime(3702):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-22 10:13:57.320: E/AndroidRuntime(3702):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-22 10:13:57.320: E/AndroidRuntime(3702):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-22 10:13:57.320: E/AndroidRuntime(3702):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-22 10:13:57.320: E/AndroidRuntime(3702):     at android.os.Looper.loop(Looper.java:137)
12-22 10:13:57.320: E/AndroidRuntime(3702):     at android.app.ActivityThread.main(ActivityThread.java:5041)
12-22 10:13:57.320: E/AndroidRuntime(3702):     at java.lang.reflect.Method.invokeNative(Native Method)
12-22 10:13:57.320: E/AndroidRuntime(3702):     at java.lang.reflect.Method.invoke(Method.java:511)
12-22 10:13:57.320: E/AndroidRuntime(3702):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-22 10:13:57.320: E/AndroidRuntime(3702):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-22 10:13:57.320: E/AndroidRuntime(3702):     at dalvik.system.NativeStart.main(Native Method)
12-22 10:13:57.320: E/AndroidRuntime(3702): Caused by: java.lang.ClassCastException: android.os.Bundle cannot be cast to classes.CycleAccount
12-22 10:13:57.320: E/AndroidRuntime(3702):     at activity.CycleAccountAccountReview.onCreate(CycleAccountAccountReview.java:77)
12-22 10:13:57.320: E/AndroidRuntime(3702):     at android.app.Activity.performCreate(Activity.java:5104)
12-22 10:13:57.320: E/AndroidRuntime(3702):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-22 10:13:57.320: E/AndroidRuntime(3702):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-22 10:13:57.320: E/AndroidRuntime(3702):     ... 11 more

任何想法?

修改

CycleItems = new CycleAccount();
documentItemResponse = JsonAcountReview.getAccountItem(params[0] ,params[1],params[2],params[3],params[4],params[5]);
CycleItems = JsonAcountReview.AccountItem(documentItemResponse);

我从Json

获取数据

1 个答案:

答案 0 :(得分:1)

试试这个......

Intent intent = new Intent(AccountReviewActivity.this, CycleAccountAccountReview.class);
intent.putExtra("data",cycleItems);
startActivity(intent);

并且收到了ACtivity

CycleAccount cycleItem = getIntent().getParcelableExtra("data");