2

I'm using a bundle to pass data between thread.
Sometime when i try to read the data's bundle i got an ArrayIndexOutOfBoundsException :

java.lang.ArrayIndexOutOfBoundsException: length=0; index=4
        at android.util.ArrayMap.indexOf(ArrayMap.java:110)
        at android.util.ArrayMap.indexOfKey(ArrayMap.java:339)
        at android.util.ArrayMap.get(ArrayMap.java:381)
        at android.os.BaseBundle.getByte(BaseBundle.java:693)
        at android.os.Bundle.getByte(Bundle.java:579)
        at android.os.BaseBundle.getByte(BaseBundle.java:680)
        at android.os.Bundle.getByte(Bundle.java:566)
        at com.online.libcommunication.net.ThreadD.handleMessage(ThreadD.java:425)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:135)
        at android.os.HandlerThread.run(HandlerThread.java:61)

The code triggering the exception is :

    @Override
    public boolean handleMessage(Message msg) {
        if(!this.isAlive()) {
            return false;
        }

        switch (msg.what)
        {
            case ThreadMessages.MSG_CMD : 
                bundle = msg.getData();
                byte modecmd = bundle.getByte("mode", (byte) 127); // <-- This is the line causing the exception
                // ...
                return true;
        }
    }

And that's how the bundle is created on the other thread :

public void sendCommand(Bundle bundle,DPO dpo)
    {
        Message msg = mPHandler.obtainMessage();
        msg.what = ThreadMessages.MSG_CMD;
        msg.obj = dpo;
        msg.setData(bundle);
        mPHandler.sendMessage(msg);
    }

    bc.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
                mCmdBundle.clear();
                mCmdBundle.putByte("mode", (byte) 6);
                // + Other datas
                mActivity.getController().sendCommand(mCmdBundle, mDPO);
        }
    });

I don't really understand what is happening , it's almost like the getX method from the bundle is not finding the associated key and throw an exception instead of a default value. The exception also happen randomly, sometime it's working fine.

I could simply wrap the all thing in a try/catch but i'd rather understand the root of the problem.

Any ideas ?

Edit : Running on Android 5.1.1

grunk
  • 14,718
  • 15
  • 67
  • 108

2 Answers2

1

After researching on your issue as it doesn't feel usual.

I found this: Google bug It's on android.util.ContainerHelpers.binarySearch

Google says they have fixed this issue in Android 5.1.

But there are similar occurrences on newer versions too.

It might be related. I hope this helps.

Deˣ
  • 4,191
  • 15
  • 24
0

Try to check if bundle contains key "mode" before taking the value. Seems it's a bug in Bundle ArrayMap

Dmytro Batyuk
  • 957
  • 8
  • 15