android_apple
android_apple

Reputation: 27

android webview can not load any url

I meet a serious problems when load url with android WebView, in case a page can not load url and the page is blank, other pages which use WebView are not load out the page and i must exit my app to reboot it, so the WebView page can load url success. I do not know why this happens?

Can somebody tell me Why and How to solve this questions?

Thank you.

Upvotes: 0

Views: 529

Answers (2)

Nikhil PV
Nikhil PV

Reputation: 1012

Try this code may work

WebViewSampleActivity.java

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class WebViewSampleActivity extends Activity {
private WebView webview;
private static final String TAG = "Main";
private ProgressDialog progressBar;  

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.main);

    this.webview = (WebView)findViewById(R.id.webView1);

    WebSettings settings = webview.getSettings();
    settings.setJavaScriptEnabled(true);
    webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

    final AlertDialog alertDialog = new AlertDialog.Builder(this).create();

    progressBar = ProgressDialog.show(WebViewSampleActivity.this, "WebView Example", "Loading...");

    webview.setWebViewClient(new WebViewClient() {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Log.i(TAG, "Processing webview url click...");
            view.loadUrl(url);
            return true;
        }

        public void onPageFinished(WebView view, String url) {
            Log.i(TAG, "Finished loading URL: " +url);
            if (progressBar.isShowing()) {
                progressBar.dismiss();
            }
        }

        @SuppressWarnings("deprecation")
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Log.e(TAG, "Error: " + description);
            Toast.makeText(WebViewSampleActivity.this, "Oh no! " + description, Toast.LENGTH_SHORT).show();
            alertDialog.setTitle("Error");
            alertDialog.setMessage(description);
            alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    return;
                }
            });
            alertDialog.show();
        }
    });
    webview.loadUrl("http://www.google.com");
  }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<WebView
    android:id="@+id/webView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1" />

</LinearLayout>

Add the internet permission in the manifest

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

Upvotes: 0

Arjun saini
Arjun saini

Reputation: 4182

Try this on Your activity in Mainfest set this

 android:onHistory="true"

if You have problem also when not result than try this code also

On Create

    webview = (WebView) findViewById(R.id.webview);
    WebSettings settings = webview.getSettings();
    settings.setJavaScriptEnabled(true);
    //ws.setJavaScriptEnabled(true);
    settings.setJavaScriptCanOpenWindowsAutomatically(true);
    webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);




 webview.setWebViewClient(new WebViewClient() {

        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Log.i(TAG, "Processing webview url click..."+url);
            view.loadUrl(url);

            return true;
        }


        public void onPageFinished(WebView view, String url) {
            Log.e(TAG, "Finished loading URL: " + url);


            //webview.reload();
        }


        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {



        }
    });

    webview.loadUrl("your Url");

Inside the class

 @Override
 public void onBackPressed() {

    if (webview.canGoBack()) {
        webview.goBack();
    } else {
        finish();
    }


 }

 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        //Display confirmation here, finish() activity.
        if (webview.canGoBack()) {
            webview.goBack();
        } else {
            finish();
        }
        return true;
    }
    return super.onKeyDown(keyCode, event);
  }

Upvotes: 2

Related Questions