【问题标题】:Why Url in not loading in my application with WebView为什么在我的应用程序中没有使用 WebView 加载 Url
【发布时间】:2013-01-06 11:18:02
【问题描述】:

我写了下面的代码,网址在默认浏览器中打开,为什么它没有加载到我的应用程序中。

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

【问题讨论】:

标签: android android-intent android-webview


【解决方案1】:

尝试在WebView 上设置WebViewClient

myWebView.setWebViewClient(new WebViewClient() {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url)
    {
        view.loadUrl(url);
        return true;
    }
});
myWebView.loadUrl("http://www.google.com");

【讨论】:

    【解决方案2】:

    也许你想要这个解决方案:

    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);
    

    别忘了添加权限:

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

    另请查看此链接:https://stackoverflow.com/a/7306176

    【讨论】:

      猜你喜欢
      • 2022-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多