• <bdo id='azGT3'></bdo><ul id='azGT3'></ul>

    1. <legend id='azGT3'><style id='azGT3'><dir id='azGT3'><q id='azGT3'></q></dir></style></legend>
    2. <i id='azGT3'><tr id='azGT3'><dt id='azGT3'><q id='azGT3'><span id='azGT3'><b id='azGT3'><form id='azGT3'><ins id='azGT3'></ins><ul id='azGT3'></ul><sub id='azGT3'></sub></form><legend id='azGT3'></legend><bdo id='azGT3'><pre id='azGT3'><center id='azGT3'></center></pre></bdo></b><th id='azGT3'></th></span></q></dt></tr></i><div id='azGT3'><tfoot id='azGT3'></tfoot><dl id='azGT3'><fieldset id='azGT3'></fieldset></dl></div>

      <small id='azGT3'></small><noframes id='azGT3'>

        <tfoot id='azGT3'></tfoot>

        readBooleanArray 抛出 RuntimeException(“错误的数组长度")

        时间:2023-10-05

      1. <tfoot id='4VJez'></tfoot>

        <legend id='4VJez'><style id='4VJez'><dir id='4VJez'><q id='4VJez'></q></dir></style></legend>
            <bdo id='4VJez'></bdo><ul id='4VJez'></ul>
            <i id='4VJez'><tr id='4VJez'><dt id='4VJez'><q id='4VJez'><span id='4VJez'><b id='4VJez'><form id='4VJez'><ins id='4VJez'></ins><ul id='4VJez'></ul><sub id='4VJez'></sub></form><legend id='4VJez'></legend><bdo id='4VJez'><pre id='4VJez'><center id='4VJez'></center></pre></bdo></b><th id='4VJez'></th></span></q></dt></tr></i><div id='4VJez'><tfoot id='4VJez'></tfoot><dl id='4VJez'><fieldset id='4VJez'></fieldset></dl></div>
                <tbody id='4VJez'></tbody>
              • <small id='4VJez'></small><noframes id='4VJez'>

                  本文介绍了readBooleanArray 抛出 RuntimeException(“错误的数组长度")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我知道 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(“错误的数组长度")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:带有 onClickListener[] 的 CheckBox[]? 下一篇:设置数组中所有对象的布尔属性

                  相关文章

                1. <small id='FiCLI'></small><noframes id='FiCLI'>

                  <tfoot id='FiCLI'></tfoot>
                2. <i id='FiCLI'><tr id='FiCLI'><dt id='FiCLI'><q id='FiCLI'><span id='FiCLI'><b id='FiCLI'><form id='FiCLI'><ins id='FiCLI'></ins><ul id='FiCLI'></ul><sub id='FiCLI'></sub></form><legend id='FiCLI'></legend><bdo id='FiCLI'><pre id='FiCLI'><center id='FiCLI'></center></pre></bdo></b><th id='FiCLI'></th></span></q></dt></tr></i><div id='FiCLI'><tfoot id='FiCLI'></tfoot><dl id='FiCLI'><fieldset id='FiCLI'></fieldset></dl></div>

                    <legend id='FiCLI'><style id='FiCLI'><dir id='FiCLI'><q id='FiCLI'></q></dir></style></legend>
                      • <bdo id='FiCLI'></bdo><ul id='FiCLI'></ul>