开发者

WebView does not display loaded Html on some phones

I have a WebView in one of my Activities where I want to load a Html page. The page contains 开发者_运维技巧jquery-mobile and some html. So I do the following in my Activity :

    mWebView=(WebView) findViewById(R.id.MyWebView);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.setWebViewClient(new WebViewClient(){
           [...]
    });
    mWebView.loadUrl("http://www.mymobilepage.html");

The problem is that the page gets loaded and displayed on the emulator, and on a HTC Desire, but when I try to load it on a LG Optimus One nothing gets displayed. The events onPageStarted and onPageFinished both get fired in my WebViewClient but just a blank page is displayed, and also I don't have any errors in my LogCat.

Thanks in advance.


When onPageFinished is called, the page may not be completely rendered. The documentation states:

Notify the host application that a page has finished loading. This method is called only for main frame. When onPageFinished() is called, the rendering picture may not be updated yet. To get the notification for the new Picture, use onNewPicture(WebView, Picture).

However, note that onNewPicture is documented as deprecated and obsolete. I ask about a replacement/alternative here.


This should be a comment, but since there is a bit of code on it I've added as response.

Try changing default background to transparent and alerting as soon as the page is loaded, just to be sure that at least the html is being interpreted:

    mWebView = (WebView) this.findViewById(R.id.webview);
    mWebView.setBackgroundColor(0);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
    mWebView.setWebViewClient(new WebViewClient(){
        @Override
        public void onPageFinished(WebView view, String url)
        {
         super.onPageFinished(view, url);
         view.loadUrl("javascript:(function() { alert('hello'); })()");
        } });

and when loading the webpage:

    mWebView.clearView();
    mWebView.loadUrl("http://yourmobilepage.something/");

and let us know if something happened.


Try this:

webView.post(new Runnable() {

        @Override
        public void run() {
            // Your code here...

        }
    });


Have you checked your html/js code with different versions on the emulator? Newer Android versions have newer versions of WebKit, that might be the problem.

I would also check if you have LogCat set to show Error messages only, or Debug+Info+Warning+Error messages. According to this, the javascript errors should show up as Debug messages.


I had a similar issue to this, I found that calling clearview and then reload seemed to clear it up -- as in:

mWebView.clearView();
mWebView.loadUrl("http://yourmobilepage.something/");
mWebView.reload();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜