From 50314f0561903e1be9b2636efd56488901ddebc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laurent=20Sava=C3=ABte?= Date: Tue, 22 May 2018 10:17:30 +0100 Subject: [PATCH] Prevent crash when keyboard opens before text input is created --- .../uimanager/RNCustomKeyboardModule.java | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/android/src/main/java/com/facebook/react/uimanager/RNCustomKeyboardModule.java b/android/src/main/java/com/facebook/react/uimanager/RNCustomKeyboardModule.java index 9c18158..0ffab14 100644 --- a/android/src/main/java/com/facebook/react/uimanager/RNCustomKeyboardModule.java +++ b/android/src/main/java/com/facebook/react/uimanager/RNCustomKeyboardModule.java @@ -26,6 +26,7 @@ import com.facebook.react.bridge.ReadableMapKeySetIterator; import com.facebook.react.bridge.UiThreadUtil; import com.facebook.react.bridge.WritableArray; +import com.facebook.react.uimanager.IllegalViewOperationException; import com.facebook.react.uimanager.RootView; import com.facebook.react.uimanager.UIManagerModule; import com.facebook.react.uimanager.UIViewOperationQueue; @@ -43,8 +44,20 @@ public RNCustomKeyboardModule(ReactApplicationContext reactContext) { Handler handle = new Handler(Looper.getMainLooper()); private ReactEditText getEditById(int id) { - UIViewOperationQueue uii = this.getReactApplicationContext().getNativeModule(UIManagerModule.class).getUIImplementation().getUIViewOperationQueue(); - return (ReactEditText) uii.getNativeViewHierarchyManager().resolveView(id); + UIViewOperationQueue uii = null; + ReactEditText edit = null; + while (edit == null) { + // we assume the native component will be instantiated at some point, so just + // loop until it appears. Most of the time, the first pass will pick it up. + uii = this.getReactApplicationContext().getNativeModule(UIManagerModule.class).getUIImplementation().getUIViewOperationQueue(); + try { + edit = (ReactEditText) uii.getNativeViewHierarchyManager().resolveView(id); + } + catch (IllegalViewOperationException e) { + // do nothing + } + } + return edit; } @ReactMethod @@ -278,4 +291,4 @@ public void run() { public String getName() { return "CustomKeyboard"; } -} \ No newline at end of file +}