我知道parcelable隱藏了一些祕密,但並沒有想到我需要知道它們,現在unitl。readBooleanArray拋出RuntimeException(「壞數組長度」)
以下是代碼之前,我有:
...
parcel.writeBooleanArray(new boolean[]{booleanValue1, booleanValue2, booleanValue3});
....
boolean[] booleans = new boolean[3];
in.readBooleanArray(booleans);
...
不知怎的,它停止工作,除了我的許多設備,所以我不能複製。於是我決定將它更改爲:
...
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);
....
包裹的源代碼:
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)
所以我的猜測,我需要的代碼更改爲:
...
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()返回boolean []的用法是什麼?也許我需要通過這個方法創建布爾數組,然後使用writeBooleanArray(boolean [])?但它對我來說沒有意義...爲什麼它在某些設備上工作,而不在其他...
在此先感謝。
剛剛嘗試使用createBooleanArray方法,它對我來說沒有意義,它是什麼對於%)。 –