我正在尝试使我的班级处于Parcelable,但我不断遇到这些错误:

    08-28 07:42:55.598    7810-7810/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
        at android.view.View$1.onClick(View.java:3617)
        at android.view.View.performClick(View.java:4222)
        at android.view.View$PerformClick.run(View.java:17620)
        at android.os.Handler.handleCallback(Handler.java:800)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:194)
        at android.app.ActivityThread.main(ActivityThread.java:5391)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
        at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at android.view.View$1.onClick(View.java:3612)
            at android.view.View.performClick(View.java:4222)
            at android.view.View$PerformClick.run(View.java:17620)
            at android.os.Handler.handleCallback(Handler.java:800)
            at android.os.Handler.dispatchMessage(Handler.java:100)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5391)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
            at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = thesleeplesselite.drgreenthumb.CropRotationPlan$SubPlan)
        at android.os.Parcel.writeSerializable(Parcel.java:1279)
        at android.os.Parcel.writeValue(Parcel.java:1233)
        at android.os.Parcel.writeList(Parcel.java:622)
        at thesleeplesselite.drgreenthumb.CropRotationPlan.writeToParcel(CropRotationPlan.java:248)
        at android.os.Parcel.writeParcelable(Parcel.java:1254)
        at android.os.Parcel.writeValue(Parcel.java:1173)
        at android.os.Parcel.writeMapInternal(Parcel.java:591)
        at android.os.Bundle.writeToParcel(Bundle.java:1627)
        at android.os.Parcel.writeBundle(Parcel.java:605)
        at android.content.Intent.writeToParcel(Intent.java:6850)
        at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1908)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1483)
        at android.app.Activity.startActivityForResult(Activity.java:3388)
        at android.app.Activity.startActivityForResult(Activity.java:3349)
        at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:820)
        at thesleeplesselite.drgreenthumb.CropRotationPlannerActivity.btnViewPlan(CropRotationPlannerActivity.java:197)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at android.view.View$1.onClick(View.java:3612)
            at android.view.View.performClick(View.java:4222)
            at android.view.View$PerformClick.run(View.java:17620)
            at android.os.Handler.handleCallback(Handler.java:800)
            at android.os.Handler.dispatchMessage(Handler.java:100)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5391)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
            at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.io.NotSerializableException: thesleeplesselite.drgreenthumb.CropRotationPlan
        at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1364)
        at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
        at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:979)
        at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:368)
        at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1074)
        at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
        at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
        at android.os.Parcel.writeSerializable(Parcel.java:1274)
            at android.os.Parcel.writeValue(Parcel.java:1233)
            at android.os.Parcel.writeList(Parcel.java:622)
            at thesleeplesselite.drgreenthumb.CropRotationPlan.writeToParcel(CropRotationPlan.java:248)
            at android.os.Parcel.writeParcelable(Parcel.java:1254)
            at android.os.Parcel.writeValue(Parcel.java:1173)
            at android.os.Parcel.writeMapInternal(Parcel.java:591)
            at android.os.Bundle.writeToParcel(Bundle.java:1627)
            at android.os.Parcel.writeBundle(Parcel.java:605)
            at android.content.Intent.writeToParcel(Intent.java:6850)
            at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1908)
            at android.app.Instrumentation.execStartActivity(Instrumentation.java:1483)
            at android.app.Activity.startActivityForResult(Activity.java:3388)
            at android.app.Activity.startActivityForResult(Activity.java:3349)
            at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:820)
            at thesleeplesselite.drgreenthumb.CropRotationPlannerActivity.btnViewPlan(CropRotationPlannerActivity.java:197)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at android.view.View$1.onClick(View.java:3612)
            at android.view.View.performClick(View.java:4222)
            at android.view.View$PerformClick.run(View.java:17620)
            at android.os.Handler.handleCallback(Handler.java:800)
            at android.os.Handler.dispatchMessage(Handler.java:100)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5391)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
            at dalvik.system.NativeStart.main(Native Method)


这是我班的结构;它有一个内部类。我认为放置整个代码并不理想。太长了

public class CropRotationPlan implements Parcelable {

    String name; // Name of plan
    HashMap<String, List<HashMap<String, String>>> listOfFamilies; // HashMap containing Plant Family Names as keys and a List of plants
    List<SubPlan> listOfSubPlans;

    public CropRotationPlan() {
        name = null;
        listOfFamilies = new HashMap<>();
        listOfSubPlans = new ArrayList<>();
    }

    ...

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.name);
        dest.writeSerializable(this.listOfFamilies);
        dest.writeList(this.listOfSubPlans);
    }

    protected CropRotationPlan(Parcel in) {
        this.name = in.readString();
        this.listOfFamilies = (HashMap<String, List<HashMap<String, String>>>) in.readSerializable();
        this.listOfSubPlans = new ArrayList<SubPlan>();
        in.readList(this.listOfSubPlans, List.class.getClassLoader());
    }

    public static final Parcelable.Creator<CropRotationPlan> CREATOR = new Parcelable.Creator<CropRotationPlan>() {
        public CropRotationPlan createFromParcel(Parcel source) {
            return new CropRotationPlan(source);
        }

        public CropRotationPlan[] newArray(int size) {
            return new CropRotationPlan[size];
        }
    };

    public class SubPlan implements Serializable {
        int year;
        HashMap<String, Integer> orderOfFamilies;
        ...
    }
}


在这一点上,我真的不知道该怎么办。

CropRotationPlan.java:248dest.writeList(this.listOfSubPlans);中的writeToParcel()

另外,在另一个类中,我有以下方法:

public void btnViewPlan(View view) {
    // We'll be sending our cropRotationPlan object to next activity (PlanActivity)
    // And we'll be expecting a result (since we'll allow the user to manipulate the object there)
    Intent i = new Intent(CropRotationPlannerActivity.this, PlanActivity.class);
    i.putExtra("cropRotationPlan", cropRotationPlan);
    startActivityForResult(i, SET_PLAN_REQUEST);
}


CropRotationPlannerActivity.java:197是:startActivityForResult(i, SET_PLAN_REQUEST);

我尝试将内部类放入一个单独的类中,然后让它实现SerializableParcelable(只是它们的其中之一),但我一直在出错。所有与我的SubPlan类有关;像是将其弄乱的问题。以及在代码中的某处使用TypedList

帮助将不胜感激。谢谢!

我只是不知道该怎么办了。

PS:对不起,如果我现在听起来不太连贯。恢复理智后,我可能会编辑此帖子。

SubPlan类的代码

public class SubPlan implements Serializable {
        int year;
        HashMap<String, Integer> orderOfFamilies;

        public SubPlan(int year) {
            orderOfFamilies = new HashMap<>();
            this.year = year;
            int temp = -1;
            for (String key : listOfFamilies.keySet()) {
                orderOfFamilies.put(key, ++temp);
            }
        }

        public SubPlan(int year, HashMap<String, Integer> orderOfFamilies) {
            this.year = year;
            this.orderOfFamilies = orderOfFamilies;
        }

        public int getYear() {
            return year;
        }

        public String getName() {
            return "Year " + year;
        }

        public String getKey(int index) {
            for (String key : orderOfFamilies.keySet()) {
                if (orderOfFamilies.get(key).equals(index))
                    return key;
            }
            return null;
        }

        public int getLocationOf(String family) {
            return orderOfFamilies.get(family);
        }

        public void setOrderOfFamily(String family, int index) {
            if (orderOfFamilies.containsKey(family))
                orderOfFamilies.put(family, index);
        }

        public void removeFamily(String family) {
            if (orderOfFamilies.containsKey(family)) {
                orderOfFamilies.remove(family);
            }
        }

        public void rotateFamilies() {
            for (int i = 0; i < getNumberOfFamilies(); i++) {
                String family = getKey(i);
                // if not last family on list
                if (orderOfFamilies.get(family) != getNumberOfFamilies() - 1)
                    setOrderOfFamily(family, i + 1);
                else
                    setOrderOfFamily(family, 0);
            }
        }
    }


另外,getNumberOfFamilies()是属于父类的方法,具有以下定义:

    public int getNumberOfFamilies() {
        return listOfFamilies.size();
    }


我最初不让SubPlan实现Serializable,这些是我得到的异常:

    08-28 08:31:07.809    8741-8741/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:3617)
            at android.view.View.performClick(View.java:4222)
            at android.view.View$PerformClick.run(View.java:17620)
            at android.os.Handler.handleCallback(Handler.java:800)
            at android.os.Handler.dispatchMessage(Handler.java:100)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5391)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at android.view.View$1.onClick(View.java:3612)
            at android.view.View.performClick(View.java:4222)
            at android.view.View$PerformClick.run(View.java:17620)
            at android.os.Handler.handleCallback(Handler.java:800)
            at android.os.Handler.dispatchMessage(Handler.java:100)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5391)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.RuntimeException: Parcel: unable to marshal value thesleeplesselite.drgreenthumb.CropRotationPlan$SubPlan@4191f010
            at android.os.Parcel.writeValue(Parcel.java:1235)
            at android.os.Parcel.writeList(Parcel.java:622)
            at thesleeplesselite.drgreenthumb.CropRotationPlan.writeToParcel(CropRotationPlan.java:249)
            at android.os.Parcel.writeParcelable(Parcel.java:1254)
            at android.os.Parcel.writeValue(Parcel.java:1173)
            at android.os.Parcel.writeMapInternal(Parcel.java:591)
            at android.os.Bundle.writeToParcel(Bundle.java:1627)
            at android.os.Parcel.writeBundle(Parcel.java:605)
            at android.content.Intent.writeToParcel(Intent.java:6850)
            at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1908)
            at android.app.Instrumentation.execStartActivity(Instrumentation.java:1483)
            at android.app.Activity.startActivityForResult(Activity.java:3388)
            at android.app.Activity.startActivityForResult(Activity.java:3349)
            at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:820)
            at thesleeplesselite.drgreenthumb.CropRotationPlannerActivity.btnViewPlan(CropRotationPlannerActivity.java:197)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at android.view.View$1.onClick(View.java:3612)
            at android.view.View.performClick(View.java:4222)
            at android.view.View$PerformClick.run(View.java:17620)
            at android.os.Handler.handleCallback(Handler.java:800)
            at android.os.Handler.dispatchMessage(Handler.java:100)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5391)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
            at dalvik.system.NativeStart.main(Native Method)
08-28 08:31:09.543     531-1643/? E/InputDispatcher﹕ channel '41d34990 thesleeplesselite.drgreenthumb/thesleeplesselite.drgreenthumb.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
08-28 08:31:09.543     531-1643/? E/InputDispatcher﹕ channel '4281f970 thesleeplesselite.drgreenthumb/thesleeplesselite.drgreenthumb.CropRotationPlannerActivity (server)' ~ Channel is unrecoverably broken and will be disposed!


另外,我尝试在Parcelable上实现SubPlan并使其具有自己的类:

08-28 08:54:59.291    9245-9245/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
        at android.view.View$1.onClick(View.java:3617)
        at android.view.View.performClick(View.java:4222)
        at android.view.View$PerformClick.run(View.java:17620)
        at android.os.Handler.handleCallback(Handler.java:800)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:194)
        at android.app.ActivityThread.main(ActivityThread.java:5391)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
        at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at android.view.View$1.onClick(View.java:3612)
            at android.view.View.performClick(View.java:4222)
            at android.view.View$PerformClick.run(View.java:17620)
            at android.os.Handler.handleCallback(Handler.java:800)
            at android.os.Handler.dispatchMessage(Handler.java:100)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5391)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
            at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.StackOverflowError
        at java.io.SerializationHandleMap.get(SerializationHandleMap.java:63)
        at java.io.ObjectOutputStream.dumpCycle(ObjectOutputStream.java:425)
        at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1566)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
        at java.util.HashMap.writeObject(HashMap.java:1015)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1053)
        at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
        at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
        at java.util.ArrayList.writeObject(ArrayList.java:644)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1053)
        at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
        at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
        at java.util.HashMap.writeObject(HashMap.java:1016)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1053)
        at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
        at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
        at android.os.Parcel.writeSerializable(Parcel.java:1274)
        at thesleeplesselite.drgreenthumb.CropRotationPlan.writeToParcel(CropRotationPlan.java:185)
        at android.os.Parcel.writeParcelable(Parcel.java:1254)
        at thesleeplesselite.drgreenthumb.SubPlan.writeToParcel(SubPlan.java:83)
        at android.os.Parcel.writeTypedList(Parcel.java:1017)
        at thesleeplesselite.drgreenthumb.CropRotationPlan.writeToParcel(CropRotationPlan.java:186)
        at android.os.Parcel.writeParcelable(Parcel.java:1254)
        at thesleeplesselite.drgreenthumb.SubPlan.writeToParcel(SubPlan.java:83)
        at android.os.Parcel.writeTypedList(Parcel.java:1017)
        at thesleeplesselite.drgreenthumb.CropRotationPlan.writeToParcel(CropRotationPlan.java:186)
        at android.os.Parcel.writeParcelable(Parcel.java:1254)
        at thesleeplesselite.drgreenthumb.SubPlan.writeToParcel(SubPlan.java:83)
        at android.os.Parcel.writeTypedList(Parcel.java:1017)
        at thesleeplesselite.drgreenthumb.CropRotationPlan.write

最佳答案

根本原因在底部列出,并且错误说明了一切:

java.io.NotSerializableException: thesleeplesselite.drgreenthumb.CropRotationPlan


CropRotationPlan必须为Serializable

更新资料

因为SubPlan不是static,所以序列化SubPlan也意味着序列化外部类CropRotationPlan。由于似乎只有SubPlan构造函数使用外部类,因此将类更改为static并将listOfFamilies作为参数发送给构造函数。

10-08 17:56