readBooleanArray throws RuntimeException(“bad array lengths”)

余生长醉 提交于 2019-11-29 12:52:16
Roger Alien

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;

Before you start reading from the Parcel instance invoke the setDataPosition method.

Eg.

in.setDataPosition(0);

If you have an array in a parcel, then the write.....Array functions first write the size of the array as an int, and then the values (in case of boolean, ints, which are either 0 or 1). So you can't get a 3-length array back, if you call the function 3 times with a 1-length array (because the first one writes 4 ints, the second one reads 6 ints).

You can get back your boolean values easily, if you call parcel.createBooleanArray(), which returns the array you put into the parcel with parcel.writeBooleanArray(boolean[]).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!