LibGdx Android 使用奇数键码 (1026) 恢复时游戏崩溃

LibGdx Android Game Crashes on resume with odd keycode (1026)

我们的 LibGdx 项目在恢复时崩溃,而 运行 在 android。

以下跟踪显示出了什么问题。但无法弄清楚如何解决这个问题。

01-11 18:37:43.643  17046-17046/com.mojo.rainbowrun.android D/AndroidRuntime﹕ Shutting down VM
01-11 18:37:43.643  17046-17046/com.mojo.rainbowrun.android W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41675d88)
01-11 18:37:43.643  17046-17046/com.mojo.rainbowrun.android E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.mojo.rainbowrun.android, PID: 17046
    java.lang.ArrayIndexOutOfBoundsException: length=256; index=1026
            at com.badlogic.gdx.backends.android.AndroidInput.onKey(AndroidInput.java:561)
            at android.view.View.dispatchKeyEvent(View.java:7791)
            at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
            at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
            at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:2042)
            at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1512)
            at android.app.Activity.dispatchKeyEvent(Activity.java:2418)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1969)
            at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:3852)
            at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3826)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
            at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
            at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
            at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3525)
            at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
            at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3582)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
            at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
            at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
            at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
            at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
            at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
            at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3558)
            at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3718)
            at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2087)
            at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1781)
            at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1772)
            at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2064)
            at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
            at android.os.MessageQueue.nativePollOnce(Native Method)
            at android.os.MessageQueue.next(MessageQueue.java:138)
            at android.os.Looper.loop(Looper.java:196)
            at android.app.ActivityThread.main(ActivityThread.java:5137)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:718)
            at dalvik.system.NativeStart.main(Native Method)

这是 AndroidInput.java 第 561 行附近的代码片段:

            if (keyCode == Keys.BUTTON_CIRCLE) {
                if (keys[Keys.BUTTON_CIRCLE]) {
                    keyCount--;
                    keys[Keys.BUTTON_CIRCLE] = false;
                }
            } else {
                if (keys[e.getKeyCode()]) { // Line 561
                    keyCount--;
                    keys[e.getKeyCode()] = false;
            }

keys 数组有 256 个条目,因此 Libgdx 不需要像 1026 这样大的键码。

在第 561 行中,if 语句有一个数组,当 if 语句条件执行时,您将获取键码作为索引变量,这可能导致数组越界,如 Logcat 所说.如果这是导致问题的原因,请尝试更改它并查看是否可以解决问题。

第 561 行 中的 if 语句else 语句一起执行。如果第一个iffalse。验证您是否可以更改该行中的代码,看看这是否是真正导致崩溃的原因,我不知道 e 对象是什么意思,因为您没有发布他的工作在你的代码中。