Android Sudoku应用挂掉的问题
在真机上测试数独游戏时发现,快速点击屏幕时,游戏偶尔出现挂死的情况,Log如下
04-08 15:35:00.838 7317-7356/org.elvalad.sudoku D/OpenGLRenderer﹕ Enabling debug mode 0 04-08 15:35:23.036 7317-7317/org.elvalad.sudoku E/InputEventReceiver﹕ Exception dispatching input event. 04-08 15:35:23.036 7317-7317/org.elvalad.sudoku E/MessageQueue-JNI﹕ Exception in MessageQueue callback: handleReceiveCallback 04-08 15:35:23.041 7317-7317/org.elvalad.sudoku E/MessageQueue-JNI﹕ java.lang.ArrayIndexOutOfBoundsException: length=81; index=83 at org.elvalad.sudoku.GameView.onTouchEvent(GameView.java:282) at android.view.View.dispatchTouchEvent(View.java:8393) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2432) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2432) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2432) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2432) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119) at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2314) at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1692) at android.app.Activity.dispatchTouchEvent(Activity.java:2749) at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2275) at android.view.View.dispatchPointerEvent(View.java:8583) at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4091) at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3957) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3509) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3562) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3528) at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3638) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3536) at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3695) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3509) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3562) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3528) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3536) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3509) at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5806) at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5745) at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5716) at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5896) at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:192) at android.os.MessageQueue.nativePollOnce(Native Method) at android.os.MessageQueue.next(MessageQueue.java:143) at android.os.Looper.loop(Looper.java:122) at android.app.ActivityThread.main(ActivityThread.java:5312) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696) 04-08 15:35:23.041 7317-7317/org.elvalad.sudoku D/AndroidRuntime﹕ Shutting down VM 04-08 15:35:23.042 7317-7317/org.elvalad.sudoku E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: org.elvalad.sudoku, PID: 7317 java.lang.ArrayIndexOutOfBoundsException: length=81; index=83 at org.elvalad.sudoku.GameView.onTouchEvent(GameView.java:282) at android.view.View.dispatchTouchEvent(View.java:8393) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2432) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2432) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2432) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2432) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119) at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2314) at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1692) at android.app.Activity.dispatchTouchEvent(Activity.java:2749) at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2275) at android.view.View.dispatchPointerEvent(View.java:8583) at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4091) at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3957) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3509) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3562) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3528) at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3638) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3536) at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3695) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3509) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3562) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3528) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3536) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3509) at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5806) at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5745) at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5716) at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5896) at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:192) at android.os.MessageQueue.nativePollOnce(Native Method) at android.os.MessageQueue.next(MessageQueue.java:143) at android.os.Looper.loop(Looper.java:122) at android.app.ActivityThread.main(ActivityThread.java:5312) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
从Log中可以很容易的发现是数组越界了,说明我们在游戏中绘制游戏格子时坐标并不是完全正确的,一个简单的处理办法是我们在使用数组时添加保护机制,当数组的Index大于81时直接让这次touch屏幕动作无效即可。
-------------------------------
问道,修仙
-------------------------------