无法编组可序列化数据的值

Unable to Marshal value on Serializable Data(无法编组可序列化数据的值)

本文介绍了无法编组可序列化数据的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试将 HashMap 传递给我的 MainActivity 类时出现以下错误(TabooCards 实现了 Serializable).我在这里读到 HashMap 是可序列化的,并且 HashMap 中包含的任何对象也必须是可序列化的.我假设 Integer 是可序列化的,并且我知道 TabooCards 是可序列化的,所以出了什么问题?

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?

代码:

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

禁忌卡片:

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)

推荐答案

使用 Parcelable 而不是 Serializable.阅读这篇tuto

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

这篇关于无法编组可序列化数据的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:无法编组可序列化数据的值

基础教程推荐