10

我正在从活动中调用 javascript 函数,但Uncaught ReferenceError: myFunction is not defined at null:1出现错误。这是我的文件

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;
    }    
}

测试.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。请帮我找到解决方案。

4

2 回答 2

20

您应该在页面加载时执行 javascript

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

代码将被执行,并会找到您的 javascript 函数。你现在做的方式不会等待。

于 2013-07-13T20:32:18.253 回答
0

这个工作只需要改变参数设置顺序

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);

        final 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());




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



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

        webview.setWebChromeClient(new WebChromeClient());
        webview.setWebViewClient(new WebViewClient() 
        {

            public void onPageFinished(WebView view, String url)
            {
                webview.loadUrl("javascript:myFunction('"+str+"')");
            }
        }
            );
        webview.loadUrl("file:///android_asset/test.html");

    }


    @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;
    }    
}
于 2013-07-13T21:10:37.010 回答