Android programming: ArrayIndexOutOfBoundsException in ViewRoot.handleMessage

I had fought with this exception for a week:

Thread [<1> main] (Suspended (exception ArrayIndexOutOfBoundsException))
ViewRoot.handleMessage(Message) line: 1895
ViewRoot(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 130
ActivityThread.main(String[]) line: 3695
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 507
ZygoteInit$MethodAndArgsCaller.run() line: 842
ZygoteInit.main(String[]) line: 600
NativeStart.main(String[]) line: not available [native method]

When I saw the exception backtrace I tought that I was doing something wrong with my View hierarchy that was damaging the state of the ViewRoot instance.

Then I realized that I was actually accessing an array with an invalid index from an event handler that is actually called from the Android class android.view.core.ViewRoot!

Then I understood what was happening. I was using an array with an invalid index in a method that was called by the onTouch event handler!

So, the exception backtrace should be something like this:

Thread [<1> main] (Suspended (exception ArrayIndexOutOfBoundsException))
MyClassHere.myMethodHere() line: 123
...
...
ViewRoot.handleMessage(Message) line: 1895
ViewRoot(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 130
ActivityThread.main(String[]) line: 3695
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 507
ZygoteInit$MethodAndArgsCaller.run() line: 842
ZygoteInit.main(String[]) line: 600
NativeStart.main(String[]) line: not available [native method]

Why isn’t my class appearing in the backtrace? I don’t know!

So, remember that, when you have an Exception throwing from ViewRoot.handleMessage probably you are raising that exception from an event handler that is called from that method!