Android Parcelable 错误的数组长度 [英] Android Parcelable bad array lengths

查看:23
本文介绍了Android Parcelable 错误的数组长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

包裹.为什么他们不能更容易?我正在尝试编写一个应用程序,该应用程序将 ArrayList 作为 Parcelable 发送.当我尝试在第二个 Activity 中获取 Intent 时,会出现错误:

Parcelables. Why can't they be more easy? I'm trying to write an application which sends an ArrayList as a parcelable. When I try to get the Intent in the second Activity, I'll get the error:

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,它也会起作用:

I've tried Strings instead of a Parcelable arraylist, this will work. Even if I get the ArrayList after setting the items, it will work:

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

但是当我想在第二个活动中获取数组列表时,我得到了之前的错误:

But as I want to get the arraylist in the second activity, I get the previous error:

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

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

What could be the error? And here is my NewsItem class if u need this:

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 中的调用顺序.

You need to read from the Parcel in the same order in which you wrote things. You are writing this.id first, but you are attempting to read the string array before reading the id. Reverse the order of the calls in either the constructor or writeToParcel.

另外,为什么不写一个字符串数组,为什么不单独写每个字符串呢?看起来简单多了.

Also, instead of writing a string array, why not just write each string individually? Seems a lot simpler.

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

这篇关于Android Parcelable 错误的数组长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆