简体   繁体   中英

Error passing parcelable object using intent

I have written the following class that implements parcelable.

package com.example.allinone;

import android.accounts.Account;
import android.os.Parcel;
import android.os.Parcelable;

public class WebSiteObject implements Parcelable {

    public String webSiteName;
    public String webSiteURL;

    public WebSiteObject(String webSiteName, String webSiteURL) throws Exception {
        this.setWebSiteName(webSiteName);
        this.setWebSiteURL(webSiteURL);
    }

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

    @Override
    public void writeToParcel(Parcel out, int arg1) {
        // TODO Auto-generated method stub
        out.writeString(webSiteName);
        out.writeString(webSiteURL);
    }

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

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

    public String getWebSiteName() {
        return webSiteName;
    }
    public String getWebSiteURL() {
        return webSiteURL;
    }
    public void setWebSiteName(String webSiteName) {
        this.webSiteName = webSiteName;
    }
    public void setWebSiteURL(String webSiteURL) {
        this.webSiteURL = webSiteURL;
    }
}

Then I used the following code to pass an object of this class from one activity to another

Intent webActivityLauncher = new Intent(MainActivity.this, WebActivity.class);
webActivityLauncher.putExtra("selectedWebObject", aWebSiteObject);
startActivity(webActivityLauncher);

And the following code to access it in the second activity

WebSiteObject aWebSiteObject = (WebSiteObject)getIntent().getParcelableExtra("selectedWebObject");

But it is not working. I am getting the following error in logcat.

05-30 17:36:12.450: E/AndroidRuntime(1140): FATAL EXCEPTION: main
05-30 17:36:12.450: E/AndroidRuntime(1140): Process: com.example.allinone, PID: 1140
05-30 17:36:12.450: E/AndroidRuntime(1140): java.lang.ClassCastException: android.accounts.Account cannot be cast to com.example.allinone.WebSiteObject
05-30 17:36:12.450: E/AndroidRuntime(1140):     at com.example.allinone.WebActivity.onCreateOptionsMenu(WebActivity.java:37)
05-30 17:36:12.450: E/AndroidRuntime(1140):     at android.app.Activity.onCreatePanelMenu(Activity.java:2538)
05-30 17:36:12.450: E/AndroidRuntime(1140):     at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:224)
05-30 17:36:12.450: E/AndroidRuntime(1140):     at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:232)
05-30 17:36:12.450: E/AndroidRuntime(1140):     at android.support.v7.app.ActionBarActivityDelegateICS.onCreatePanelMenu(ActionBarActivityDelegateICS.java:147)
05-30 17:36:12.450: E/AndroidRuntime(1140):     at android.support.v7.app.ActionBarActivity.onCreatePanelMenu(ActionBarActivity.java:199)
05-30 17:36:12.450: E/AndroidRuntime(1140):     at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onCreatePanelMenu(ActionBarActivityDelegateICS.java:285)
05-30 17:36:12.450: E/AndroidRuntime(1140):     at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:436)
05-30 17:36:12.450: E/AndroidRuntime(1140):     at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:800)
05-30 17:36:12.450: E/AndroidRuntime(1140):     at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:221)
05-30 17:36:12.450: E/AndroidRuntime(1140):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
05-30 17:36:12.450: E/AndroidRuntime(1140):     at android.view.Choreographer.doCallbacks(Choreographer.java:574)
05-30 17:36:12.450: E/AndroidRuntime(1140):     at android.view.Choreographer.doFrame(Choreographer.java:543)
05-30 17:36:12.450: E/AndroidRuntime(1140):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
05-30 17:36:12.450: E/AndroidRuntime(1140):     at android.os.Handler.handleCallback(Handler.java:733)
05-30 17:36:12.450: E/AndroidRuntime(1140):     at android.os.Handler.dispatchMessage(Handler.java:95)
05-30 17:36:12.450: E/AndroidRuntime(1140):     at android.os.Looper.loop(Looper.java:136)
05-30 17:36:12.450: E/AndroidRuntime(1140):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-30 17:36:12.450: E/AndroidRuntime(1140):     at java.lang.reflect.Method.invokeNative(Native Method)
05-30 17:36:12.450: E/AndroidRuntime(1140):     at java.lang.reflect.Method.invoke(Method.java:515)
05-30 17:36:12.450: E/AndroidRuntime(1140):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-30 17:36:12.450: E/AndroidRuntime(1140):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-30 17:36:12.450: E/AndroidRuntime(1140):     at dalvik.system.NativeStart.main(Native Method)

Line 37 in WebActivity.java is WebSiteObject aWebSiteObject = (WebSiteObject)getIntent().getParcelableExtra("selectedWebObject"); I have set a break point on line 37. It is hitting the break point, but the code is not running beyond this, that is line 38 is never executed. What is wrong here? How can I rectify this?

Two problems..

  1. CREATOR should have WebSiteObject

As below

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

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

2.Add constructor

public WebSiteObject(Parcel in) {
        // Read all fields in the same sequence as you have written into writeToParcel 
       webSiteName = in.in.readString();
       webSiteURL = in.readString();
    } 

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