2015-11-15 118 views
0

我是Android开发人员的初学者。所以我在网上搜索教程来了解about.I获取了我在我的计算机上尝试的代码,当我运行代码并查看应用程序时,它说网页不可用,因此我添加了清单文件,但它仍然说同样的事情无法在WebView上加载网页

的代码如下:

package com.paresh.webviewclientdemo; 

import android.app.Activity; 
import android.graphics.Bitmap; 
import android.os.Bundle; 
import android.view.KeyEvent; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 

public class WebViewClientDemoActivity extends Activity { 
    WebView web; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     web = (WebView) findViewById(R.id.webview01); 
     web.setWebViewClient(new myWebClient()); 
     web.getSettings().setJavaScriptEnabled(true); 
     web.loadUrl("https://www.google.com"); 
    } 

    public class myWebClient extends WebViewClient { 
     @Override 
     public void onPageStarted(WebView view, String url, Bitmap favicon) { 
      super.onPageStarted(view, url, favicon); 
     } 

     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      view.loadUrl(url); 
      return true; 
     } 
    } 
} 

布局:

<?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 xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/webview01" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:layout_weight="1" /> 
</LinearLayout> 

这里的清单文件:

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

但即使在此之后,Android网络视图网页无法加载,我找不到该错误。帮我找到它。我是否需要添加其他内容,或者在我给出的现有文件中是否存在错误

回答

0

请尝试使用这些代码。

类:

public class WebViewClientDemoActivity extends Activity { 

private WebView webView; 
private String url = "https://www.google.com"; 
private ProgressBar progressBar; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    progressBar = (ProgressBar) findViewById(R.id.progressBar1); 
    webView = (WebView) findViewById(R.id.webView1); 
    webView.getSettings().setJavaScriptEnabled(true); 
    webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); 
    webView.getSettings().setDomStorageEnabled(true); 
    webView.getSettings().setBuiltInZoomControls(true); 
    webView.setWebViewClient(new myWebClient()); 
    webView.loadUrl(url); 

} 

public class myWebClient extends WebViewClient { 
    @Override 
    public void onPageStarted(WebView view, String url, Bitmap favicon) { 
     super.onPageStarted(view, url, favicon); 
    } 

    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     view.loadUrl(url); 
     return true; 
    } 

    @Override 
    public void onPageFinished(WebView view, String url) { 
     super.onPageFinished(view, url); 
     Handler handler = new Handler(); 
     handler.postDelayed(new Runnable() { 
      public void run() { 
       progressBar.setVisibility(View.GONE); 
      } 
     }, 10000); 
    } 
} 

main.xml中

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

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

<ProgressBar 
    android:id="@+id/progressBar1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:indeterminate="false"/> 
</RelativeLayout>