【发布时间】: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