尝试将HashMap传递给我的MainActivity类时,我收到以下错误(TabooCards实现了Serializable)。我在这里读到HashMap是可序列化的,并且HashMap中包含的任何对象也必须是可序列化的。我认为Integer
是可序列化的,我知道TabooCards
是可序列化的,所以出了什么问题?
代码:
AsyncTask onPostExecute:
protected void onPostExecute(HashMap<Integer, TabooCard> result) {
super.onPostExecute(result);
Intent i = new Intent(getBaseContext(), MainActivity.class);
i.putExtra("TABOO_CARDS", tabooCards);
startActivity(i);
}
TabooCards:
public class TabooCard implements Serializable{
/**
*
*/
private static final long serialVersionUID = -6996042490365349623L;
String mainWord, taboo1, taboo2, taboo3, taboo4, taboo5;
public TabooCard() {
this("Main Card", "Taboo 1", "Taboo 2", "Taboo 3", "Taboo 4", "Taboo 5");
}
public TabooCard(String mainword, String taboo1, String taboo2, String taboo3, String taboo4, String taboo5) {
setMainWord(mainword);
setTaboo1(taboo1);
setTaboo2(taboo2);
setTaboo3(taboo3);
setTaboo4(taboo4);
setTaboo5(taboo5);
}
//Various getter/setter methods
logcat的
E/AndroidRuntime(28441): FATAL EXCEPTION: main
E/AndroidRuntime(28441): Process: com.jaytj.taboo, PID: 28441
E/AndroidRuntime(28441): java.lang.RuntimeException: Parcel: unable to marshal value com.jaytj.taboo.TabooCard@42d2fce8
E/AndroidRuntime(28441): at android.os.Parcel.writeValue(Parcel.java:1266)
E/AndroidRuntime(28441): at android.os.Parcel.writeMapInternal(Parcel.java:594)
E/AndroidRuntime(28441): at android.os.Parcel.writeMap(Parcel.java:578)
E/AndroidRuntime(28441): at android.os.Parcel.writeValue(Parcel.java:1197)
E/AndroidRuntime(28441): at android.os.Parcel.writeArrayMapInternal(Parcel.java:618)
E/AndroidRuntime(28441): at android.os.Bundle.writeToParcel(Bundle.java:1692)
E/AndroidRuntime(28441): at android.os.Parcel.writeBundle(Parcel.java:636)
E/AndroidRuntime(28441): at android.content.Intent.writeToParcel(Intent.java:7410)
E/AndroidRuntime(28441): at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2289)
E/AndroidRuntime(28441): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1437)
E/AndroidRuntime(28441): at android.app.Activity.startActivityForResult(Activity.java:3511)
E/AndroidRuntime(28441): at android.app.Activity.startActivityForResult(Activity.java:3472)
E/AndroidRuntime(28441): at android.app.Activity.startActivity(Activity.java:3714)
E/AndroidRuntime(28441): at android.app.Activity.startActivity(Activity.java:3682)
E/AndroidRuntime(28441): at com.jaytj.taboo.SplashActivity$AsyncWordGrabber.onPostExecute(SplashActivity.java:47)
E/AndroidRuntime(28441): at com.jaytj.taboo.SplashActivity$AsyncWordGrabber.onPostExecute(SplashActivity.java:1)
E/AndroidRuntime(28441): at android.os.AsyncTask.finish(AsyncTask.java:632)
E/AndroidRuntime(28441): at android.os.AsyncTask.access$600(AsyncTask.java:177)
E/AndroidRuntime(28441): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
E/AndroidRuntime(28441): at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime(28441): at android.os.Looper.loop(Looper.java:157)
E/AndroidRuntime(28441): at android.app.ActivityThread.main(ActivityThread.java:5356)
E/AndroidRuntime(28441): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(28441): at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime(28441): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
E/AndroidRuntime(28441): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
E/AndroidRuntime(28441): at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
E/AndroidRuntime(28441): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:5)
使用Parcelable而不是Serializable。 阅读此tuto
这就是你的课程应该如何:
import android.os.Parcel;
import android.os.Parcelable;
public class TabooCard implements Parcelable {
public static final Parcelable.Creator<TabooCard> CREATOR = new Parcelable.Creator<TabooCard>()
{
@Override
public TabooCard createFromParcel(Parcel source)
{
return new TabooCard(source);
}
@Override
public TabooCard[] newArray(int size)
{
return new TabooCard[size];
}
};
String mainWord, taboo1, taboo2, taboo3, taboo4, taboo5;
public TabooCard() {
this("Main Card", "Taboo 1", "Taboo 2", "Taboo 3", "Taboo 4", "Taboo 5");
}
public TabooCard(String mainword, String taboo1, String taboo2, String taboo3, String taboo4, String taboo5) {
setMainWord(mainword);
setTaboo1(taboo1);
setTaboo2(taboo2);
setTaboo3(taboo3);
setTaboo4(taboo4);
setTaboo5(taboo5);
}
public TabooCard(Parcel in) {
setMainWord(in.readString());
setTaboo1(in.readString());
setTaboo2(in.readString());
setTaboo3(in.readString());
setTaboo4(in.readString());
setTaboo5(in.readString());
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(mainWord);
dest.writeString(taboo1);
dest.writeString(taboo2);
dest.writeString(taboo3);
dest.writeString(taboo4);
dest.writeString(taboo5);
}