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

前端之家收集整理的这篇文章主要介绍了android – Web视图不加载重定向网址前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的网页浏览不适用于重定向到其他网页的网址.它从应用程序打开浏览器,但不加载webview.任何的想法?

webview的代码是:

  1. webView = (WebView) findViewById(R.id.simpleWebView);
  2. WebSettings webSettings = webView.getSettings();
  3. webSettings.setJavaScriptEnabled(true);
  4.  
  5. webView.setWebViewClient(new WebViewClient());
  6.  
  7. if(getIntent().getStringExtra("url")!=null){
  8. loadLink(getIntent().getStringExtra("url"));
  9. }else{
  10. Toast.makeText(WebViewActivity.this,"Please try again later!",Toast.LENGTH_SHORT).show();
  11. finish();
  12. }

解决方法

主类
  1. public class Main extends Activity {
  2. private WebView webview;
  3. private static final String TAG = "Main";
  4. private ProgressDialog progressBar;
  5.  
  6. /** Called when the activity is first created. */
  7. @Override
  8. public void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10.  
  11. requestWindowFeature(Window.FEATURE_NO_TITLE);
  12.  
  13. setContentView(R.layout.main);
  14.  
  15. this.webview = (WebView)findViewById(R.id.webview);
  16.  
  17. WebSettings settings = webview.getSettings();
  18. settings.setJavaScriptEnabled(true);
  19. webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
  20.  
  21. final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
  22.  
  23. progressBar = ProgressDialog.show(Main.this,"WebView Example","Loading...");
  24.  
  25. webview.setWebViewClient(new WebViewClient() {
  26. public boolean shouldOverrideUrlLoading(WebView view,String url) {
  27. Log.i(TAG,"Processing webview url click...");
  28. view.loadUrl(url);
  29. return true;
  30. }
  31.  
  32. public void onPageFinished(WebView view,"Finished loading URL: " +url);
  33. if (progressBar.isShowing()) {
  34. progressBar.dismiss();
  35. }
  36. }
  37.  
  38. public void onReceivedError(WebView view,int errorCode,String description,String failingUrl) {
  39. Log.e(TAG,"Error: " + description);
  40. Toast.makeText(activity,"Oh no! " + description,Toast.LENGTH_SHORT).show();
  41. alertDialog.setTitle("Error");
  42. alertDialog.setMessage(description);
  43. alertDialog.setButton("OK",new DialogInterface.OnClickListener() {
  44. public void onClick(DialogInterface dialog,int which) {
  45. return;
  46. }
  47. });
  48. alertDialog.show();
  49. }
  50. });
  51. webview.loadUrl("http://www.google.com");
  52. }
  53. }

你的main.xml布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <WebView android:id="@string/webview"
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent"
  10. android:layout_weight="1" />
  11. </LinearLayout>

猜你在找的Android相关文章