简体   繁体   English

在webview中加载网页(android)

[英]load web page in webview (android)

i am trying to load a web page in webview but it is not loading web page in webview instead it is asking another browser app to load web page 我试图加载网页webview ,但它不是在加载网页webview相反,它是要求其他浏览器的应用程序加载网页

i want to load webpage in my app webview 我想在我的应用程序webview加载网页

this is my code: it ask another app to load page 这是我的代码:它要求另一个应用程序加载页面

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

    }

}

You have not set webviewclient so please set it using below code. 您尚未设置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");

}

You are missing the following line. 您缺少以下行。 Add it after calling the id of the webview, then it will work : 在调用webview的ID后添加它,然后它将起作用:

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

You can try this, Hope it will help you. 您可以尝试此方法,希望它会对您有所帮助。

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

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM