简体   繁体   中英

Class cast Exception while sending object to another activity

I try to send one object with Intent .

I have three class, class A has a list of Class B and class B has a list of class C, and i want send class A from one activity to another with the Parcelable . i try following code:

in Class A:

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);
}

in class 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);

}

and in class 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);
}

and for sending and getting data I use following code:

Sending:

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

Getting:

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

and i get following Error:

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

any ideas?

Edit

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

I get data from Json

try this...

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

and in received ACtivity

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM