java - Android Parcelable 错误数组长度

标签 java android arraylist parcelable

包裹。为什么他们不能更容易?我正在尝试编写一个将 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)

我试过使用字符串而不是 Parcelable arraylist,这会起作用。即使我在设置项目后得到 ArrayList,它也会起作用:

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

但是因为我想在第二个 Activity 中获取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];
        }
    };
}

最佳答案

您需要按照写入内容的相同顺序从 Parcel 中读取。您首先编写了 this.id,但您试图在读取 id 之前读取字符串数组。反转构造函数或 writeToParcel 中的调用顺序。

此外,为什么不单独编写每个字符串,而不是编写一个字符串数组?看起来简单多了。

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

关于java - Android Parcelable 错误数组长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20829975/

相关文章:

javascript - 如何使用 jjs 参数启动 java nashorn

android - 是否可以在 android 消息中添加动画表情符号

android - Surfaceflinger 测试程序

java - ArrayList仅重复打印最后一个成员

java - 如果它更接近我设置的值,有没有办法打印它?

java - 使用继承扩展java中的类

java - 从用户输入创建点对象

Java比较两个List的对象值?

java - 从 Java 应用程序登录到 ELK,无需解析日志

android - React Native - 未优化的 APK 将我的应用程序提交到 Google Play