网络视图未加载重定向网址

时间:2016-05-03 11:51:48

标签: android webview

我的网页浏览功能不适用于重定向到其他网页的网址。它从应用程序打开浏览器,但不加载webview。有什么想法吗?

webview的代码是:

webView = (WebView) findViewById(R.id.simpleWebView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);

webView.setWebViewClient(new WebViewClient());

if(getIntent().getStringExtra("url")!=null){
    loadLink(getIntent().getStringExtra("url"));
}else{
    Toast.makeText(WebViewActivity.this, "Please try again later!", 
    Toast.LENGTH_SHORT).show();
    finish();
}

5 个答案:

答案 0 :(得分:4)

主类

public class Main 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.webview);

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

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

        progressBar = ProgressDialog.show(Main.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();
                }
            }

            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Log.e(TAG, "Error: " + description);
                Toast.makeText(activity, "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:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <WebView android:id="@string/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1" />
</LinearLayout>

答案 1 :(得分:1)

尝试覆盖Web视图客户端方法,这将有助于您

mWebView.setWebViewClient(new WebViewClient() {


        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
           // Here put your code
              Log.d("My Webview", url);

           // return true; //Indicates WebView to NOT load the url;
              return false; //Allow WebView to load url
        }
    });

答案 2 :(得分:0)

我认为你应该写下你的loadLink方法包含的内容。 你确定,你已经正确实现了它 - webView.loadUrl(...)?

答案 3 :(得分:0)

  webView=(WebView)view.findViewById(R.id.webview);
        webView.setWebViewClient(new MyBrowser());
        webView.getSettings().setLoadsImagesAutomatically(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        webView.loadUrl("https://www.google.co.in/");
        return view;
    }
    private class MyBrowser extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            pDialog.dismiss();
        }
    }

答案 4 :(得分:-2)

试试这个

if(getIntent().getStringExtra("url")!=null){ 
 mWebView.loadUrl(getIntent().getStringExtra("url"));
}

并检查您是否获得了互联网许可