gpt4 book ai didi

java - 使用 Intent 传递可分割对象时出错

转载 作者:行者123 更新时间:2023-12-02 05:47:11 25 4
gpt4 key购买 nike

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

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

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

以下代码可在第二个 Activity 中访问它

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 行永远不会执行。这里有什么问题吗?我该如何纠正这个问题?

最佳答案

两个问题..

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

关于java - 使用 Intent 传递可分割对象时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23954300/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com