Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
571 views
in Technique[技术] by (71.8m points)

android - readBooleanArray throws RuntimeException("bad array lengths")

I knew that parcelable are hide something secret, but didn't thought that i need to know them, unitl now.

Here is the code i had before:

...
parcel.writeBooleanArray(new boolean[]{booleanValue1, booleanValue2, booleanValue3});
....

boolean[] booleans = new boolean[3];
in.readBooleanArray(booleans);
...

Somehow it stops working on many devices except my, so i can't reproduce it. Then i decided to change it to:

        ...
    parcel.writeBooleanArray(new boolean[]{booleanValue1});
    parcel.writeBooleanArray(new boolean[]{booleanValue2});
    parcel.writeBooleanArray(new boolean[]{booleanValue3});
        ...

    boolean[] booleans1 = new boolean[1];
    boolean[] booleans2 = new boolean[1];
    boolean[] booleans3 = new boolean[1];
    in.readBooleanArray(booleans1);
    in.readBooleanArray(booleans2); // it crashes here
    in.readBooleanArray(booleans3);
        ....

Source code of Parcel:

public final void readBooleanArray(boolean[] val) {
    int N = readInt();
    if (N == val.length) {
        for (int i=0; i<N; i++) {
            val[i] = readInt() != 0;
        }
    } else {
        throw new RuntimeException("bad array lengths");
    }
}

LogCat errors:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my/com.my.activities.MyActivity}: java.lang.RuntimeException: bad array lengths
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
    at android.app.ActivityThread.access$600(ActivityThread.java:128)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4517)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: bad array lengths
    at android.os.Parcel.readBooleanArray(Parcel.java:619)

So my guess that i need to change the code to:

...
parcel.writeBooleanArray(new boolean[]{booleanValue1, booleanValue2, booleanValue3});
....

boolean[] booleans1 = new boolean[1];
boolean[] booleans2 = new boolean[1];
boolean[] booleans3 = new boolean[1];
in.readBooleanArray(booleans1);
in.readBooleanArray(booleans2);
in.readBooleanArray(booleans3);
....

But will it help?

Also what is the usage of Parcel.createBooleanArray() returns boolean[]; Maybe i need to create boolean array via this method and then use writeBooleanArray(boolean[])? But it doesn't make sense to me... why it's working on some devices and doesn't on other...

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Actually i found solution for my problem, but not answer in another question:

Here is how can You work with booleans in Pracelable:

.....
// Write:
out.writeByte((byte) (booleanValue ? 1 : 0));

....

// Read:
boolValue = in.readByte() == 1;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...