Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript not working in a WebView Activity

I have an Activity that has just a WebView, which contains HTML, CSS and Javascript Code.

It seems that there's a problem with the access of Javascript to the screen size of the view. LogCat says:

(Tag: Web Console): Uncaught TypeError: Cannot call method 'getWidth' of undefined 
at file:///android_asset/Prospekte/grund/html5-player/js/epaper-mobile.js:20

When I look into the js-file, there is: var f=this.body.getWidth();

There curious thing is that sometimes the code works. The epaper is shown well. But most time there's this error.

    setContentView(R.layout.prospekt_layout);
    final Activity activity = this;

    mWebView = (WebView) findViewById(R.id.prospekt_webview);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setDomStorageEnabled(true);
    mWebView.setWebChromeClient(new WebChromeClient() {
    public void onProgressChanged(WebView view, int progress) {
        activity.setProgress(progress * 1000);
      }
    });

    mWebView.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;
        }
    });   

    mWebView.loadUrl("file:///android_asset/Prospekte/modKachel/mobile.html");    

The layout is:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    >
<WebView
    android:id="@+id/prospekt_webview"
    android:layout_width="900px"
    android:layout_height="900px"
/>
</LinearLayout>

I changed the size of the webView cause I thought that this could be the solution..but it's no working with dp either.

Someone has in idea?

like image 253
10ff Avatar asked Jan 03 '12 10:01

10ff


1 Answers

Set this setting as well for your webView:

WebSettings settings = webView.getSettings();
settings.setDomStorageEnabled(true);

For Detail refer to answer in the following link: ERROR/Web Console: Uncaught TypeError: Cannot call method 'getItem' of null at http://m.youtube.com/:844

Update: or adding this might help:

webView.loadDataWithBaseURL("fake://fake.com", myString, "text/html", "UTF-8", null);
like image 147
Usama Sarwar Avatar answered Sep 28 '22 03:09

Usama Sarwar