Android Parcelable数组长度不对

Parcelables并。 为什么他们不能更容易? 我试图编写一个应用程序发送ArrayList作为parcelable。 当我尝试在第二个Activity中获得Intent时,我会得到这个错误:

12-29 21:36:08.158: W/System.err(20117): java.lang.RuntimeException: bad array lengths 12-29 21:36:08.158: W/System.err(20117): at android.os.Parcel.readStringArray(Parcel.java:967) 12-29 21:36:08.158: W/System.err(20117): at net.sutomaji.sv.helperclasses.NewsItem.<init>(NewsItem.java:102) 12-29 21:36:08.158: W/System.err(20117): at net.sutomaji.sv.helperclasses.NewsItem$1.createFromParcel(NewsItem.java:129) 12-29 21:36:08.158: W/System.err(20117): at net.sutomaji.sv.helperclasses.NewsItem$1.createFromParcel(NewsItem.java:1) 12-29 21:36:08.158: W/System.err(20117): at android.os.Parcel.readParcelable(Parcel.java:2104) 12-29 21:36:08.158: W/System.err(20117): at android.os.Parcel.readValue(Parcel.java:2013) 12-29 21:36:08.158: W/System.err(20117): at android.os.Parcel.readListInternal(Parcel.java:2343) 12-29 21:36:08.158: W/System.err(20117): at android.os.Parcel.readArrayList(Parcel.java:1703) 12-29 21:36:08.158: W/System.err(20117): at android.os.Parcel.readValue(Parcel.java:2034) 12-29 21:36:08.158: W/System.err(20117): at android.os.Parcel.readArrayMapInternal(Parcel.java:2314) 12-29 21:36:08.158: W/System.err(20117): at android.os.Bundle.unparcel(Bundle.java:249) 12-29 21:36:08.158: W/System.err(20117): at android.os.Bundle.getParcelableArrayList(Bundle.java:1250) 12-29 21:36:08.158: W/System.err(20117): at android.content.Intent.getParcelableArrayListExtra(Intent.java:4680) 12-29 21:36:08.158: W/System.err(20117): at net.sutomaji.sv.MainActivity.onCreate(MainActivity.java:104) 12-29 21:36:08.158: W/System.err(20117): at android.app.Activity.performCreate(Activity.java:5241) 12-29 21:36:08.158: W/System.err(20117): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 12-29 21:36:08.158: W/System.err(20117): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162) 12-29 21:36:08.158: W/System.err(20117): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2248) 12-29 21:36:08.158: W/System.err(20117): at android.app.ActivityThread.access$800(ActivityThread.java:138) 12-29 21:36:08.158: W/System.err(20117): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199) 12-29 21:36:08.158: W/System.err(20117): at android.os.Handler.dispatchMessage(Handler.java:102) 12-29 21:36:08.158: W/System.err(20117): at android.os.Looper.loop(Looper.java:136) 12-29 21:36:08.158: W/System.err(20117): at android.app.ActivityThread.main(ActivityThread.java:5050) 12-29 21:36:08.168: W/System.err(20117): at java.lang.reflect.Method.invokeNative(Native Method) 12-29 21:36:08.168: W/System.err(20117): at java.lang.reflect.Method.invoke(Method.java:515) 12-29 21:36:08.168: W/System.err(20117): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 12-29 21:36:08.168: W/System.err(20117): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 12-29 21:36:08.168: W/System.err(20117): at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126) 12-29 21:36:08.168: W/System.err(20117): at dalvik.system.NativeStart.main(Native Method) 

我已经尝试了string,而不是一个parcelable arraylist,这将工作。 即使我在设置项目后得到ArrayList,它也会工作:

 mainIntent.putParcelableArrayListExtra("items", items); for(Parcelable p : mainIntent.getParcelableArrayListExtra("items")) { NewsItem ni = (NewsItem) p; Log.v(TAG, ni.getName()); } 

但是,因为我想在第二个活动中获取arraylist,所以我得到了以前的错误:

 for(Parcelable p : getIntent().getParcelableArrayListExtra("items")) { NewsItem ni = (NewsItem) p; Log.v(TAG, ni.getName()); } 

什么可能是错误? 这里是我的NewsItem类,如果你需要这个:

 package net.sutomaji.sv.helperclasses; imports... public class NewsItem implements Parcelable { private int id; private String name; private String bubble; private String drawable; private String title; private String summary; private String description; public NewsItem() { this.bubble = ""; this.drawable = null; this.title = ""; this.summary = ""; this.id = -1; this.name = ""; this.description = ""; } public NewsItem(int id, String name, String bubble, String drawable, String title, String summary, String description) { this.id = id; this.bubble = bubble; this.drawable = drawable; this.title = title; this.summary = summary; this.name = name; this.description = description; } getters and setters... /* PARCELLING STUFF....................... */ public NewsItem(Parcel in) { String[] data = new String[6]; in.readStringArray(data); in.readInt(); this.name = data[0]; this.bubble = data[1]; this.drawable = data[2]; this.title = data[3]; this.summary = data[4]; this.description = data[5]; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeInt(this.id); dest.writeStringArray(new String[] { this.name, this.bubble, this.drawable, this.title, this.summary, this.description }); } public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { public NewsItem createFromParcel(Parcel in) { return new NewsItem(in); } public NewsItem[] newArray(int size) { return new NewsItem[size]; } }; } 

Solutions Collecting From Web of "Android Parcelable数组长度不对"

你需要按照你写的东西的顺序从Parcel里读取。 您首先要编写this.id ,但是在读取id之前,您正在尝试读取string数组。 在构造函数或writeToParcel调用writeToParcel

此外,而不是写一个string数组,为什么不写每个string单独? 似乎很简单。

 public NewsItem(Parcel in) { in.readInt(); this.name = in.readString(); this.bubble = in.readString(); this.drawable = in.readString(); this.title = in.readString(); this.summary = in.readString(); this.description = in.readString(); } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeInt(this.id); dest.writeString(this.name); dest.writeString(this.bubble); dest.writeString(this.drawable); dest.writeString(this.title); dest.writeString(this.summary); dest.writeString(this.description); }