Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
500 views
in Technique[技术] by (71.8m points)

java - Unable to Marshal value on Serializable Data

I get the following error when trying to pass a HashMap to my MainActivity class (TabooCards implements Serializable). I read around here that HashMap is serializable, and that any Objects contained in the HashMap must also be serializable. I assume Integer is serializable, and I know TabooCards is serializable, so what's going wrong?

Code:

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use Parcelable and not Serializable. Read this tuto

This is how your class should be :

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...