I (think I) have the same problem as described in this SO and also this SO :
I have a WebView in an TabGroupActivity. When I load a remote URL containing a SELECT element, and I tap on it, it crashes on my Nexus S running Android 4.1.2 (and I read the same reports from a user running a Samsung Galaxy S2 on 5.1.1 and a Nexus 5 on 6.0.0).
The exception is:
android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@4160b960 is not valid; is your activity running?
This is the structure:
// MobileWebTab.java
public class MobileWebTab extends TabGroupActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startChildActivity("Mobile_Activity", new Intent(this,Mobile_Web_Activity.class));
}
}
// Mobile_Web_Activity.java
public class Mobile_Web_Activity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mobile_web);
}
}
// MobileWebView.java
public class MobileWebView extends RelativeLayout {
Context _context;
private void initializeView(Context context){
_context=context;
LayoutInflater layoutInflater = LayoutInflater.from(context);
layoutInflater.inflate(R.layout.layout_mobile_site, this);
WebView mWebview = (WebView)findViewById(R.id.ww_mobile_site);
mWebview.setWebViewClient(new WebViewClient() {
//If you will not use this method url links are opeen in new brower not in webview
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
mWebview.loadUrl(Constants.app_mobile_site);
}
public MobileWebView(Context context) {
super(context);
initializeView(context);
}
public MobileWebView(Context context, AttributeSet attrs) {
super(context, attrs);
initializeView(context);
}
public MobileWebView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initializeView(context);
}
}
Both answers recommend to set the parent activity context for the WebView - but how and where do I do that, when the WebView is defined via XML?