未捕获的Ref​​erenceError:myFunction是不是空定义为:在web视图1的Andr​​oid异常 [英] Uncaught ReferenceError: myFunction is not defined at null:1 Android exception in webview

查看:291
本文介绍了未捕获的Ref​​erenceError:myFunction是不是空定义为:在web视图1的Andr​​oid异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打电话从活动中的JavaScript功能,但我正在逐渐未捕获的Ref​​erenceError:1 错误:myFunction是不是空定义。下面是我的文件

I am calling javascript function from an activity but I am getting Uncaught ReferenceError: myFunction is not defined at null:1 error. Here is my files

MainActivity.java

MainActivity.java

package com.example.androidexample;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        WebView webview = (WebView)this.findViewById(R.id.webView1);
        WebSettings webSettings = webview.getSettings();

        // Enable Javascript for interaction
        webSettings.setJavaScriptEnabled(true);

        // Make the zoom controls visible
        webSettings.setBuiltInZoomControls(true);

        // Allow for touching selecting/deselecting data series
        webview.requestFocusFromTouch();

        // Set the client
        webview.setWebViewClient(new WebViewClient());
        webview.setWebChromeClient(new WebChromeClient());


        //webview.loadUrl("file:///android_asset/test.html");

        /*String str = "<books>" +
                "<book title=\"A Tale of Two Cities\"/>" +
                "<book title=\"1984\"/>" +
                "</books>";*/




        webview.loadUrl("file:///android_asset/test.html");
        webview.loadUrl("javascript:myFunction()");
        //webview.loadUrl("javascript:myFunction("+str+")");

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }    
}

test.html的

test.html

<html>
<body>
<script type="text/javascript" src="marknote.js"></script>
<script type="text/javascript">
function myFunction()
{
var str = '<books><book></book></books>';
var parser = new marknote.Parser();
var doc = parser.parse(str);

var bookEls = doc.getRootElement().getChildElements();

    for (var i=0; i<bookEls.length; i++) {
        var bookEl = bookEls[i];

        alert("Element name is " + bookEl.getName());
    }
}
</script>

</body>
</html>

我已经看到了,当我使用 webview.loadUrl发生这个错误(JavaScript的:myFunction的()); 。我想通过从Java XML字符串并解析它为JavaScript。请帮我找到一个解决方案。

I have seen this error occurred when I am using webview.loadUrl("javascript:myFunction()");. I want to pass a xml string from java and parse it into javascript. Please help me to find a solution.

推荐答案

在加载页面时,您应该执行JavaScript

You should execute the javascript when the page is loaded

webview.setWebViewClient(new WebViewClient(){
    public void onPageFinished(WebView view, String url){   
        webview.loadUrl("javascript:myFunction()");
    }           
});

在code会被执行,会发现你的JavaScript功能。你现在正在做的方式不会等待。

The code will be executed and will find your javascript function. The way you are doing now does not wait.

这篇关于未捕获的Ref​​erenceError:myFunction是不是空定义为:在web视图1的Andr​​oid异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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