简体   繁体   中英

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

i want to load webpage in my app 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.

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 :

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

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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