android WebView中的弹出窗口

时间:2014-03-24 11:02:24

标签: android android-intent

我正在使用webview在我的应用中打开一个链接。该链接在webview中正常工作,但是当我点击了一个打开弹出窗口的webview项目(链接打开)时,我的应用程序崩溃了。并给我错误。

03-24 16:40:10.680: W/System.err(5798): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@421e0760 is not valid; is your activity running?
03-24 16:40:10.680: W/System.err(5798):     at android.view.ViewRootImpl.setView(ViewRootImpl.java:536)
03-24 16:40:10.680: W/System.err(5798):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259)
03-24 16:40:10.680: W/System.err(5798):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
03-24 16:40:10.680: W/System.err(5798):     at android.app.Dialog.show(Dialog.java:286)
03-24 16:40:10.690: W/System.err(5798):     at com.android.org.chromium.content.browser.input.SelectPopupDialog.show(SelectPopupDialog.java:217)
03-24 16:40:10.690: W/System.err(5798):     at com.android.org.chromium.content.browser.ContentViewCore.showSelectPopup(ContentViewCore.java:2413)
03-24 16:40:10.690: W/System.err(5798):     at com.android.org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
03-24 16:40:10.690: W/System.err(5798):     at com.android.org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:27)
03-24 16:40:10.690: W/System.err(5798):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-24 16:40:10.690: W/System.err(5798):     at android.os.Looper.loop(Looper.java:136)
03-24 16:40:10.690: W/System.err(5798):     at android.app.ActivityThread.main(ActivityThread.java:5017)
03-24 16:40:10.690: W/System.err(5798):     at java.lang.reflect.Method.invokeNative(Native Method)
03-24 16:40:10.690: W/System.err(5798):     at java.lang.reflect.Method.invoke(Method.java:515)
03-24 16:40:10.690: W/System.err(5798):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-24 16:40:10.690: W/System.err(5798):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-24 16:40:10.690: W/System.err(5798):     at dalvik.system.NativeStart.main(Native Method)

我正在打开这样的网页视图。

Intent it = new Intent(MyMainActivity.this,WebViewActivity.class);
                    it.putExtra("url", url);
                    ParentActivity parent = (ParentActivity) getParent();
                    parent.startChildActivity("WebViewActivity", it);

我的WebViewActivity代码就是这个。

String url = in.getStringExtra("url");

        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setUseWideViewPort(true);

        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webView.setWebViewClient(client);

        webView.loadUrl(url);

我可以知道实现目标的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

您可以通过编程方式添加webview,而不是布局文件。

  1. 用LinearLayout(或任何适当的)替换WebView并获取该布局的实例。

  2. 动态创建新的WebView。

  3. 以下代码:

        @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        urls = getIntent().getStringExtra("key_url");
        linearWeb = (LinearLayout) findViewById(R.id.webviewLayout);
        webView = new WebView(getParent());
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setDomStorageEnabled(true);
        progressBar.setVisibility(View.GONE);
        linearWeb.addView(webView);
        showBox();
    }
    
    public void showWebView() {
        if (!((Activity) getParent()).isFinishing()) {
            setUpAction();
        }
    }
    
    private void setUpAction() {
    
        if (!TextUtils.isEmpty(urls)) {
    
            webView.loadUrl(urls);
            this.webView.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);
                    return true;
                }
            });
    
            this.webView.setWebViewClient(new WebViewClient() {
    
                @Override
                public void onPageStarted(WebView view, String url,
                        Bitmap favicon) {
    
                    progressBar.setVisibility(View.VISIBLE);
    
                }
    
                public void onReceivedError(WebView view, int errorCode,
                        String description, String failingUrl) {
                    progressBar.setVisibility(View.GONE);
                }
    
                @Override
                public void onPageFinished(WebView view, String url) {
                    progressBar.setVisibility(View.GONE);
    
                }
            });
        }
    }