diff --git a/java/src/com/android/inputmethod/keyboard/KeyDetector.java b/java/src/com/android/inputmethod/keyboard/KeyDetector.java
index 03d9defa0ae312538bcf7611cbfc129571f42407..87368d4ef1b137ea67fbf031979cef2e680969e2 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyDetector.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyDetector.java
@@ -86,6 +86,9 @@ public class KeyDetector {
      * @return the key that the touch point hits.
      */
     public Key detectHitKey(final int x, final int y) {
+        if (mKeyboard == null) {
+            return null;
+        }
         final int touchX = getTouchX(x);
         final int touchY = getTouchY(y);