android webview无法加载任何网址

时间:2016-07-26 04:28:11

标签: android url android-webview

我在使用Android WebView加载网址时遇到严重问题,如果网页无法加载网址且页面为空白,则其他使用网页的网页不会加载到网页上,我必须退出我的应用重新启动它,所以WebView页面可以加载url成功。我不知道为什么会这样?

有人可以告诉我为什么以及如何解决这个问题?

谢谢。

2 个答案:

答案 0 :(得分:2)

尝试使用您在Mainfest中的活动设置此

 android:onHistory="true"

如果您在没有结果时也遇到问题而不是尝试此代码

  

创建

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

课程内

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

答案 1 :(得分:0)

尝试此代码可能有效

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>

在清单中添加互联网权限

    <uses-permission android:name="android.permission.INTERNET"/>
相关问题