【问题标题】:Error passing parcelable object using intent使用意图传递可打包对象时出错
【发布时间】:2014-05-30 12:09:59
【问题描述】:

我编写了以下实现 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;
    }
}

然后我使用以下代码将此类的对象从一个活动传递到另一个活动

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

下面的代码在第二个活动中访问它

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

但它不起作用。我在 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)

WebActivity.java 中的第 37 行是 WebSiteObject aWebSiteObject = (WebSiteObject)getIntent().getParcelableExtra("selectedWebObject"); 我在第 37 行设置了一个断点。它正在到达断点,但代码没有超出此范围,即第 38 行永远不会执行。 这里有什么问题?我该如何纠正这个问题?

【问题讨论】:

    标签: java android android-intent parcelable


    【解决方案1】:

    两个问题..

    1. CREATOR 应该有WebSiteObject

    如下

    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.添加构造函数

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

    【讨论】:

    • 我应该在构造函数中写什么?留空?
    • @HarikrishnanT 更新了答案。检查一下。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-05
    • 1970-01-01
    • 2013-11-06
    • 1970-01-01
    • 2018-02-23
    • 2023-03-30
    • 1970-01-01
    相关资源
    最近更新 更多