diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardView.java b/java/src/com/android/inputmethod/keyboard/KeyboardView.java
index d23b8ff0eb1a46e8d1f30756004b014f27582477..1641dc4a5d68eea083d7e5aa1a3450f96f8f0b0a 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyboardView.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyboardView.java
@@ -136,6 +136,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
         @Override
         public void handleMessage(Message msg) {
             final KeyboardView keyboardView = getOuterInstance();
+            if (keyboardView == null) return;
             final PointerTracker tracker = (PointerTracker) msg.obj;
             switch (msg.what) {
             case MSG_SHOW_KEY_PREVIEW:
@@ -150,8 +151,9 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
         }
 
         public void showKeyPreview(long delay, int keyIndex, PointerTracker tracker) {
-            final KeyboardView keyboardView = getOuterInstance();
             removeMessages(MSG_SHOW_KEY_PREVIEW);
+            final KeyboardView keyboardView = getOuterInstance();
+            if (keyboardView == null) return;
             if (keyboardView.mPreviewText.getVisibility() == VISIBLE || delay == 0) {
                 // Show right away, if it's already visible and finger is moving around
                 keyboardView.showKey(keyIndex, tracker);