我知道 Parcelable 隐藏着一些秘密,但没想到我现在需要知道它们.
I knew that parcelable are hide something secret, but didn't thought that i need to know them, unitl now.
这是我之前的代码:
...
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);
....
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 错误:
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);
....
但这会有帮助吗?
Parcel.createBooleanArray() return boolean[] 的用法还有什么?也许我需要通过这种方法创建布尔数组,然后使用 writeBooleanArray(boolean[])?但这对我来说没有意义...为什么它可以在某些设备上运行而在其他设备上却不行...
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...
提前致谢.
实际上我找到了解决问题的方法,但在另一个问题中没有答案 问题:
Actually i found solution for my problem, but not answer in another question:
以下是您如何在 Pracelable 中使用布尔值:
Here is how can You work with booleans in Pracelable:
.....
// Write:
out.writeByte((byte) (booleanValue ? 1 : 0));
....
// Read:
boolValue = in.readByte() == 1;
这篇关于readBooleanArray 抛出 RuntimeException(“错误的数组长度")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!