我已经编写了以下实现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行。
怎么了我该如何纠正?
最佳答案
两个问题
创建者应具有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();
}