android中的webview问题

时间:2011-09-21 16:45:59

标签: android webview

我的应用中有一个webview。我可以在标题栏上看到网站的标题(我有自定义标题栏)。但是,我在视图中看不到任何内容 - 网站无法查看:( ..有什么建议吗?这是代码:

public class WebViewer extends Activity {

WebView webView;

@Override
public void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);
    setContentView(R.layout.viewer);

    webView = (WebView) findViewById(R.id.webview);

    String url = "http://www.google.com";

    final TextView title=(TextView) findViewById(R.id.title_text_view_success3);

    webView.getSettings().setJavaScriptEnabled(true);

    webView.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress)
        {
            title.setText("Loading...");
            WebViewer.this.setProgress(progress * 100);

            if(progress == 100)
            title.setText(webView.getTitle());
        }
    });

    webView.setWebViewClient(new WebViewClient() {
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
        {
            // Handle the error
        }

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

    webView.loadUrl(url);

}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
        webView.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

}

1 个答案:

答案 0 :(得分:1)

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

这不是必需的。在加载url之前调用shouldOverrideUrlLoading,以便您自己处理加载。你正在做的是一遍又一遍地加载网址。

http://developer.android.com/reference/android/webkit/WebViewClient.html#shouldOverrideUrlLoading%28android.webkit.WebView,%20java.lang.String%29