gpt4 book ai didi

java - 使用自定义对象列表(在内部类中定义)的 Parcelable 类中的问题

转载 作者:太空宇宙 更新时间:2023-11-04 13:30:19 24 4
gpt4 key购买 nike

我正在尝试使我的类可打包,但我不断收到这些错误:

    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)

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

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相关的内容类(class);比如弄脏它之类的问题。随着使用 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这些是我得到的异常(exception):

    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!

此外,我尝试实现 ParcelableSubPlan并使其拥有自己的类:

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 不是静态,所以序列化 SubPlan 也意味着序列化外部类 CropRotationPlan。由于似乎只有 SubPlan 构造函数使用外部类,因此将类更改为 static 并将 listOfFamilies 作为构造函数的参数发送。

关于java - 使用自定义对象列表(在内部类中定义)的 Parcelable 类中的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32261275/

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