Android的Parcelable坏数组的长度数组、长度、Android、Parcelable

由网友(隐形王冠i)分享简介:parcelables的。他们为什么不能更容易吗?我试图写它发送一个ArrayList作为parcelable的应用程序。当我尝试在第二个活动的意图,我会得到错误:12-29 21:36:08.158:W / System.err的(20117):java.lang.RuntimeException的:坏数组的长度...

parcelables的。他们为什么不能更容易吗?我试图写它发送一个ArrayList作为parcelable的应用程序。当我尝试在第二个活动的意图,我会得到错误:

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

我已经试过字符串,而不是一个Parcelable ArrayList的,这会工作。即使我得到的ArrayList中设置项目后,它将工作:

  mainIntent.putParcelableArrayListExtra(项目,项目);
对于(Parcelable号码:mainIntent.getParcelableArrayListExtra(项目)){
  NewsItem NI =(NewsItem)P;
  Log.v(TAG,ni.getName());
}
 

不过,我想在第二个活动ArrayList中,我得到了previous错误:

 的(Parcelable号码:getIntent()getParcelableArrayListExtra(项目)。){
  NewsItem NI =(NewsItem)P;
  Log.v(TAG,ni.getName());
}
 
android 获取数组大小,看得见的数据结构Android版之数组表 数据结构篇

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

 包net.sutomaji.sv.helperclasses;

进口...

公共类NewsItem实现Parcelable {

    私人诠释身份证;
    私人字符串名称;
    私人字符串泡沫;
    私人字符串绘制;
    私人字符串名称;
    私人字符串摘要;
    私人字符串描述;

    公共NewsItem(){
        this.bubble =;
        this.drawable = NULL;
        this.title =;
        this.summary =;
        this.id = -1;
        this.name =;
        this.description =;
    }

    公共NewsItem(INT ID,字符串名称,字符串泡沫,绘制字符串,字符串标题,摘要字符串,字符串描述){
        this.id = ID;
        this.bubble =泡沫;
        this.drawable =绘制;
        this.title =称号;
        this.summary =摘要;
        this.name =名称;
        this.description =描述;
    }


    getter和setter ...

    / *瓜分STUFF ....................... * /
    公共NewsItem(包裹中){
        的String []数据=新的String [6];

        in.readStringArray(数据);
        in.readInt();

        this.name =数据[0];
        this.bubble =数据[1];
        this.drawable =数据[2];
        this.title =数据[3];
        this.summary =数据[4];
        this.description =数据[5];
    }

    @覆盖
    公众诠释describeContents(){
        返回0;
    }

    @覆盖
    公共无效writeToParcel(包裹DEST,INT标志){
        dest.writeInt(this.id);
        dest.writeStringArray(新的String [] {
                this.name,this.bubble,this.drawable,
                this.title,this.summary,this.description
        });
    }

    公共静态最终Parcelable.Creator CREATOR =新Parcelable.Creator(){
        公共NewsItem createFromParcel(包裹中){
            返回新NewsItem(中);
        }

        公共NewsItem [] newArray(INT尺寸){
            返回新NewsItem【尺寸】;
        }
    };
}
 

解决方案

您需要从包裹在你写的东西同样的顺序来读取。你写 this.id 第一,但你正在尝试读取ID之前阅读的字符串数组。反向无论是在构造函数中调用的命令或 writeToParcel

此外,而不是写一个字符串数组,为什么不单独写每个字符串?似乎简单了很多。

 公共NewsItem(包裹中){
    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();
}

@覆盖
公共无效writeToParcel(包裹DEST,INT标志){
    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);
}
 

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)

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

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

解决方案

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

阅读全文

相关推荐

最新文章