使用Javascript不工作在web视图活动 [英] Javascript not working in a WebView Activity

查看:109
本文介绍了使用Javascript不工作在web视图活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动,刚一的WebView ,其中包含HTML,CSS和Javascript code。

似乎有与在访问问题 Javascript的以视图的屏幕尺寸。 LogCat中说:

 (标签:Web控制台):未捕获的类型错误:不能调用方法'的getWidth'未定义
在文件:///android_asset/Prospekte/grund/html5-player/js/epaper-mobile.js:20
 

当我考虑了JS文件,有:变种F = this.body.getWidth();

有奇怪的是,有时候code工作。该电子纸显示良好。但大多数时候有这个错误。

 的setContentView(R.layout.prospekt_layout);
    最后活动活动=这一点;

    mWebView =(web视图)findViewById(R.id.prospekt_webview);
    mWebView.getSettings()setJavaScriptEnabled(真)。
    mWebView.getSettings()setDomStorageEnabled(真)。
    mWebView.setWebChromeClient(新WebChromeClient(){
    公共无效onProgressChanged(web视图来看,INT进度){
        activity.setProgress(进度* 1000);
      }
    });

    mWebView.setWebViewClient(新WebViewClient(){
        @覆盖
        公共无效onReceivedError(web视图来看,INT错误code,字符串描述,字符串failingUrl)
        {
            //处理错误
        }

        @覆盖
        公共布尔shouldOverrideUrlLoading(web视图查看,字符串URL)
        {
            view.loadUrl(URL);
            返回true;
        }
    });

    mWebView.loadUrl(文件:///android_asset/Prospekte/modKachel/mobile.html);
 

的布局是:

 < XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_height =match_parent
    机器人:layout_width =match_parent
    >
<的WebView
    机器人:ID =@ + ID / prospekt_webview
    机器人:layout_width =900px
    机器人:layout_height =900px
/>
< / LinearLayout中>
 

我改变了web视图的大小,因为我认为这可能是solution..but它要么与DP没有工作。

有人有想法?

解决方案

你的web视图设置此设置以及

  WebSettings设置= webView.getSettings();
settings.setDomStorageEnabled(真正的);
 

有关详细信息请参考下面的链接来回答:

<$p$p><$c$c>http://stackoverflow.com/questions/5822256/error-web-console-uncaught-typeerror-cannot-call-method-getitem-of-null-at-h

更新: 或者加入这可能帮助:

  webView.loadDataWithBaseURL(假://fake.com,MyString的,text / html的,UTF-8,NULL);
 

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?

解决方案

Set this setting as well for your webView:

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

For Detail refer to answer in the following link:

http://stackoverflow.com/questions/5822256/error-web-console-uncaught-typeerror-cannot-call-method-getitem-of-null-at-h

Update: or adding this might help:

webView.loadDataWithBaseURL("fake://fake.com", myString, "text/html", "UTF-8", null);

这篇关于使用Javascript不工作在web视图活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆