Fix NullPointerException (#4212)
diff --git a/packages/webview_flutter/webview_flutter_android/CHANGELOG.md b/packages/webview_flutter/webview_flutter_android/CHANGELOG.md
index 176028f..b241be6 100644
--- a/packages/webview_flutter/webview_flutter_android/CHANGELOG.md
+++ b/packages/webview_flutter/webview_flutter_android/CHANGELOG.md
@@ -1,3 +1,8 @@
+## 2.2.1
+
+* Fix `NullPointerException` from a race condition when changing focus. This only affects `WebView`
+when it is created without Hybrid Composition.
+
## 2.2.0
* Implemented new `runJavascript` and `runJavascriptReturningResult` methods in platform interface.
diff --git a/packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/InputAwareWebView.java b/packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/InputAwareWebView.java
index 0a4cb3e..1276ac8 100644
--- a/packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/InputAwareWebView.java
+++ b/packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/InputAwareWebView.java
@@ -158,7 +158,7 @@
* <p>{@code targetView} should have a {@link View#getHandler} method with the thread that future
* InputConnections should be created on.
*/
- private void setInputConnectionTarget(final View targetView) {
+ void setInputConnectionTarget(final View targetView) {
if (containerView == null) {
Log.e(
TAG,
@@ -171,6 +171,13 @@
new Runnable() {
@Override
public void run() {
+ if (containerView == null) {
+ Log.e(
+ TAG,
+ "Can't set the input connection target because there is no containerView to use as a handler.");
+ return;
+ }
+
InputMethodManager imm =
(InputMethodManager) getContext().getSystemService(INPUT_METHOD_SERVICE);
// This is a hack to make InputMethodManager believe that the target view now has focus.
diff --git a/packages/webview_flutter/webview_flutter_android/android/src/test/java/io/flutter/plugins/webviewflutter/InputAwareWebViewTest.java b/packages/webview_flutter/webview_flutter_android/android/src/test/java/io/flutter/plugins/webviewflutter/InputAwareWebViewTest.java
new file mode 100644
index 0000000..0eb078d
--- /dev/null
+++ b/packages/webview_flutter/webview_flutter_android/android/src/test/java/io/flutter/plugins/webviewflutter/InputAwareWebViewTest.java
@@ -0,0 +1,48 @@
+// Copyright 2013 The Flutter Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package io.flutter.plugins.webviewflutter;
+
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.ArgumentMatchers.anyBoolean;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+
+import android.content.Context;
+import android.view.View;
+import org.junit.Test;
+
+public class InputAwareWebViewTest {
+ static class TestView extends View {
+ Runnable postAction;
+
+ public TestView(Context context) {
+ super(context);
+ }
+
+ @Override
+ public boolean post(Runnable action) {
+ postAction = action;
+ return true;
+ }
+ }
+
+ @Test
+ public void runnableChecksContainerViewIsNull() {
+ final Context mockContext = mock(Context.class);
+
+ final TestView containerView = new TestView(mockContext);
+ final InputAwareWebView inputAwareWebView = new InputAwareWebView(mockContext, containerView);
+
+ final View mockProxyAdapterView = mock(View.class);
+
+ inputAwareWebView.setInputConnectionTarget(mockProxyAdapterView);
+ inputAwareWebView.setContainerView(null);
+
+ assertNotNull(containerView.postAction);
+ containerView.postAction.run();
+ verify(mockProxyAdapterView, never()).onWindowFocusChanged(anyBoolean());
+ }
+}
diff --git a/packages/webview_flutter/webview_flutter_android/pubspec.yaml b/packages/webview_flutter/webview_flutter_android/pubspec.yaml
index ac208a0..9f9998e 100644
--- a/packages/webview_flutter/webview_flutter_android/pubspec.yaml
+++ b/packages/webview_flutter/webview_flutter_android/pubspec.yaml
@@ -2,7 +2,7 @@
description: A Flutter plugin that provides a WebView widget on Android.
repository: https://github.com/flutter/plugins/tree/master/packages/webview_flutter/webview_flutter_android
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview%22
-version: 2.2.0
+version: 2.2.1
environment:
sdk: ">=2.14.0 <3.0.0"