【问题标题】:load web page in webview (android)在 webview (android) 中加载网页
【发布时间】:2014-09-09 12:20:47
【问题描述】:

我正在尝试在 webview 中加载网页,但它没有在 webview 中加载网页,而是要求另一个浏览器应用加载网页

我想在我的应用程序中加载网页webview

这是我的代码:它要求另一个应用加载页面

package com.example.webview;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.webkit.WebView;

public class MainActivity extends ActionBarActivity {

    private WebView browser;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        browser = (WebView) findViewById(R.id.webView1);
        browser.loadUrl("http://www.google.com");

    }

}

【问题讨论】:

标签: android webview


【解决方案1】:

您还没有设置webviewclient,所以请使用下面的代码进行设置。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    browser = (WebView) findViewById(R.id.webView1);
    browser .setWebViewClient(new WebViewClient() {
        public void onReceivedError(WebView view, int errorCode, String description, String   failingUrl) {

        }
    });

    browser.loadUrl("http://www.google.com");

}

【讨论】:

    【解决方案2】:

    您缺少以下行。调用 id 后添加 webview,然后它将起作用:

    browser.setWebViewClient(new WebViewClient() {
       public void onReceivedError(WebView view, int errorCode, String description, String   failingUrl) {
          .....
       }
    

    【讨论】:

      【解决方案3】:

      你可以试试这个,希望对你有帮助。

      import android.app.Activity;
      import android.os.Bundle;
      import android.webkit.WebView;
      import android.webkit.WebViewClient;
      import android.widget.Toast;
      
      public class Main extends Activity {
      
          private WebView mWebview ;
      
          @Override
          public void onCreate(Bundle savedInstanceState) {
      
              super.onCreate(savedInstanceState);
      
              mWebview  = new WebView(this);
      
              mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
      
              final Activity activity = this;
      
              mWebview.setWebViewClient(new WebViewClient() {
                  public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                      Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
                  }
              });
      
              mWebview .loadUrl("http://www.google.com");
              setContentView(mWebview );
      
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 2013-02-02
        • 2014-03-18
        • 1970-01-01
        • 1970-01-01
        • 2011-08-22
        • 1970-01-01
        • 2017-06-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多