当前位置: 首页 > 面试题库 >

Android Parcelable错误数组长度

常乐
2023-03-14
问题内容

包裹。他们为什么不能更轻松?我正在尝试编写一个将ArrayList作为包裹发送的应用程序。当我尝试在第二个活动中获取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)

我已经尝试过使用Strings而不是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];
        }
    };
}

问题答案:

您需要以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);
}


 类似资料:
  • 新数组列表 我用多头填充这个数组列表。 当我试图将其插入postgresql时,我使用以下方法: 使用JDBC模板。但是当我试图运行这段代码时,它在“ARRAY[?]”上给出了一个错误。但如果temp2是一个单一的数字,比如:1253214,它就可以工作。有人有主意吗? 我的错误是: PreparedStatementCallback;错误的SQL语法[UPDATE pb1plnitm SET p

  • 这个错误开始突然出现,当我登录到我的应用程序(通过firebase)时,它必须显示一个列表,然而,当登录时,它没有显示任何东西,并且这个错误开始在日志中出现几次 日志: 我看不到这个错误来自什么activity或片段,我只知道当我之后登录到我的列表时,它在列表中没有显示任何东西并且这些错误在日志中以一行的形式出现

  • 我实现了本网站给出的策展人领袖选举示例 我没有增加策展人客户的数量,而是只添加了一个策展人客户,如下所示 } 我只有一个zookeeper实例,我正在使用zookeeper 3.4.6、curator framework 4.0.0和curator recipes 4.0.0。当我启动客户端时,它会连接到zookeeper,在日志中我可以看到“状态更改:已连接”消息。 然后我等了10秒,开始领导人

  • 嗨,我正在做一个类似太空入侵者的游戏,当一颗子弹与敌人相撞时,我遇到了一个问题。代码如下: 碰撞代码: Hitbox的代码: 错误消息: 我认为2发或更多子弹击中敌人造成的错误 玩家可以一次射出太多的子弹,如果他们保持空间,子弹将继续射出而不会延迟。我可以限制子弹的生成,但问题是即使屏幕上最多有10颗子弹,错误仍然会发生,我尝试用 但它只是睡了所有的游戏线程

  • 问题内容: 继承人错误: 这是导致它的代码: 图标是文件数组。奇怪的是,我的计算机可以在任何图像查看器中正常读取图像。谷歌搜索错误没有给我任何结果。我有很多图像需要阅读,因此除了将图像转换为BufferedImage之外,它们是获取图像尺寸的一种替代方法吗?这样可以解决问题吗?有没有办法修复这些图像?我是通过从iOS设备收集应用程序图标来获得它们的。使用我自己的设备进行的测试没有产生任何错误,尽管

  • 问题内容: int[][][] inputs; 这是我程序的摘录,我不知道为什么会导致错误。这不正确吗? 提前致谢 :-) 问题答案: 在Eclipse中,我得到了一个非常清晰的错误消息: 提供数组初始值设定项时,无法定义维表达式。 这意味着,可以 任一 指定的尺寸 或 数组初始化(即,值)。您 不能同时 指定两者。 只需将您的最后一行更改为