android – Web视图不加载重定向网址

前端之家收集整理的这篇文章主要介绍了android – Web视图不加载重定向网址前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的网页浏览不适用于重定向到其他网页的网址.它从应用程序打开浏览器,但不加载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();
}

解决方法

主类
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,"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>
原文链接:https://www.f2er.com/android/315619.html

猜你在找的Android相关文章