Why Url in not loading in my application with WebV

2019-08-30 09:29发布

whey i wrote below code, the url is opening in the default browser, why its not loading in the my app.

setContentView(R.layout.activity_main);
myWebView = (WebView) findViewById(R.id.webView);
myWebView.loadUrl("http://www.google.com");

2条回答
虎瘦雄心在
2楼-- · 2019-08-30 09:43

Try setting a WebViewClient on the WebView:

myWebView.setWebViewClient(new WebViewClient() {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url)
    {
        view.loadUrl(url);
        return true;
    }
});
myWebView.loadUrl("http://www.google.com");
查看更多
劫难
3楼-- · 2019-08-30 09:51

Maybe you want this solution:

WebView webview = findViewById(R.id.webviewLW);
webview = new WebView(this);
webview.getSettings().setLoadsImagesAutomatically(true);
webview.getSettings().setJavaScriptEnabled(true);
webview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

webviewLWComplience.setWebViewClient(new WebViewClient() {
        @SuppressWarnings("deprecation")
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Toast.makeText(MainActivity.this, description, Toast.LENGTH_SHORT).show();
        }

        @TargetApi(android.os.Build.VERSION_CODES.M)
        @Override
        public void onReceivedError(WebView view, WebResourceRequest req, WebResourceError rerr) {
            // Redirect to deprecated method, so you can use it in all SDK versions
            onReceivedError(view, rerr.getErrorCode(), rerr.getDescription().toString(), req.getUrl().toString());
        }
    });

webviewLWComplience.loadUrl(CONSTANTS.HOST_URL);
setContentView(webviewLWComplience);

Don't forget to add permission:

<uses-permission android:name="android.permission.INTERNET"/>

Also check this link: https://stackoverflow.com/a/7306176

查看更多
登录 后发表回答