I am trying to display a pop up window with a webview inside it having few HTML text inputs. When I focus the first input box the keyboard comes up and everything is fine. When I try to focus the next input box my app crashes with the below exception.
01-21 20:31:19.736: W/System.err(14374): android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@2a54119c is not valid; is your activity running?
01-21 20:31:19.737: W/System.err(14374): at android.view.ViewRootImpl.setView(ViewRootImpl.java:562)
01-21 20:31:19.737: W/System.err(14374): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:272)
01-21 20:31:19.737: W/System.err(14374): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
01-21 20:31:19.737: W/System.err(14374): at android.widget.PopupWindow.invokePopup(PopupWindow.java:1058)
01-21 20:31:19.737: W/System.err(14374): at android.widget.PopupWindow.showAtLocation(PopupWindow.java:887)
01-21 20:31:19.737: W/System.err(14374): at android.widget.PopupWindow.showAtLocation(PopupWindow.java:851)
01-21 20:31:19.737: W/System.err(14374): at com.android.org.chromium.content.browser.input.HandleView.showContainer(HandleView.java:176)
01-21 20:31:19.737: W/System.err(14374): at com.android.org.chromium.content.browser.input.HandleView.show(HandleView.java:190)
01-21 20:31:19.738: W/System.err(14374): at com.android.org.chromium.content.browser.input.InsertionHandleController.showHandleIfNeeded(InsertionHandleController.java:191)
01-21 20:31:19.738: W/System.err(14374): at com.android.org.chromium.content.browser.input.InsertionHandleController.showHandle(InsertionHandleController.java:67)
01-21 20:31:19.738: W/System.err(14374): at com.android.org.chromium.content.browser.ContentViewCore$10.showHandle(ContentViewCore.java:2067)
01-21 20:31:19.738: W/System.err(14374): at com.android.org.chromium.content.browser.input.InsertionHandleController.onCursorPositionChanged(InsertionHandleController.java:91)
01-21 20:31:19.738: W/System.err(14374): at com.android.org.chromium.content.browser.ContentViewCore.onSelectionBoundsChanged(ContentViewCore.java:2557)
01-21 20:31:19.738: W/System.err(14374): at com.android.org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
01-21 20:31:19.738: W/System.err(14374): at com.android.org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:28)
01-21 20:31:19.738: W/System.err(14374): at android.os.Handler.dispatchMessage(Handler.java:102)
01-21 20:31:19.738: W/System.err(14374): at android.os.Looper.loop(Looper.java:135)
01-21 20:31:19.738: W/System.err(14374): at android.app.ActivityThread.main(ActivityThread.java:5223)
01-21 20:31:19.738: W/System.err(14374): at java.lang.reflect.Method.invoke(Native Method)
01-21 20:31:19.738: W/System.err(14374): at java.lang.reflect.Method.invoke(Method.java:372)
01-21 20:31:19.738: W/System.err(14374): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
I have seen similar problem reports here in stack overflow and the web, and followed all the possible suggestions with no luck. The strange part is that this issue happens only on devices running android 4.4 and later. It works on 4.3, 4.2 etc. I have followed the suggestions provided Exception when focusing a EditText in a PopupWindow running on Device but none of them works for me. I cannot use a Dialog instead of a popup because I want the popup window to be positioned at some specific location on the screen which is not possible with dialog. Also tried all other suggestions including turning off auto suggest, adding additional attributes to the input element like autocomplete="off", autocorrect="off" but none of them worked.
Another thing which I noticed is that, if I have any other type of HTML element in between the two text boxes, say a drop down, navigating from the first input element to the drop down and then to the next input element will not crash the app and will work fine. Basically when the keyboard is presented by the first input box on android 4.4 and above, the pop up window's token manager is going for a toss. How can I solve this? Is it a bug in Android 4.4 and above? If so is there any work around?
Code :
public class MainActivity extends Activity {
private PopupWindow pw;
View contentView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
contentView = inflater.inflate(R.layout.popup, null, false);
pw = new PopupWindow(contentView, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, true);
loadContentView(contentView);
}
public void showPopUp(View view){
pw.showAtLocation(contentView, Gravity.CENTER, 10, 10);
}
public void loadContentView(View view){
WebView mWebView = new WebView(this);
mWebView.loadUrl("file:///android_asset/test.html");
LinearLayout ll = (LinearLayout)view.findViewById(R.id.contentLayout);
ll.addView(mWebView);
}
}
Popup Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:orientation="vertical">
<LinearLayout
android:id="@+id/contentLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" android:background="#00ff00"/>
</LinearLayout>
Html
<html>
<head>
<meta name=viewport content="width=device-width, initial-scale=1">
</head>
<body>
<h1 style="color:blue">This is a heading</h1>
<p style="color:red">This is a paragraph.</p>
first text ::<input type="text" autocorrect="off" autocapitalize="off" autocomplete="off"/>
second text ::<input type="text" autocorrect="off" autocapitalize="off" autocomplete="off"/>
</body>
</html>