0

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?

Community
  • 1
  • 1
thomers
  • 2,603
  • 4
  • 29
  • 50
  • They might've downvoted just from the title, as this probably won't be of help to future users. `ActivityGroup` has been deprecated since API 13. It's recommended to use `Fragment`s instead. – Mike M. Jan 21 '16 at 09:48
  • Thanks Mike - I didnt notice that, I'm not even using ActivityGroup but TabGroupActivity. Still, the short explanation couldn't have hurt him/her, right? – thomers Jan 21 '16 at 09:50
  • @thomers...yep, sometimes I got the feeling that some downvoters just vote down to have more points than others. I can´t see any reason for downvoting Your question, also the title is clear for me.... – Opiatefuchs Jan 21 '16 at 09:57
  • Maybe it´s the context...which context You are passing? – Opiatefuchs Jan 21 '16 at 10:00
  • Yeah, but `TabGroupActivity extends ActivityGroup`, so, ya know, same thing. I would bet that that's the root of the problem, too, looking at the Exception message. `ActivityGroup` is just a really bizarre implementation. – Mike M. Jan 21 '16 at 10:01
  • Mike - Hmm I see. The code is not mine, and coming from the days where we had to support Android 2.x, I assume. – thomers Jan 21 '16 at 10:04
  • Opiatefuchs - Don't know, where do I check that? – thomers Jan 21 '16 at 10:04

0 Answers0