无法从JS执行我的java方法

时间:2017-10-13 06:15:54

标签: codenameone

我仔细阅读了该主题的其他主题,但仍然无法让我的代码工作,因此寻求帮助。

以下是代码:

    BrowserComponent bc = new BrowserComponent();
    bc.setDebugMode(true);
    bc.setPage("<html><body></body></html>", null);
    JavascriptContext context = new JavascriptContext(bc);
    bc.addWebEventListener("onLoad", e -> {
        {
            JSObject syso = (JSObject) context.get("{}");
            syso.set("print", new JSFunction() {
                @Override
                public void apply(JSObject self, Object[] args) {
                    System.out.println(StringUtils.injectArgs("{0} {1} {2} {3} {4} {5} {6} {7} {8} {9}", args));
                }
            });
            context.set("window.syso", syso);
        }

        {
            JSObject syso = (JSObject) context.get("syso");
            syso.call("print", "I", "am", "in", "syso");
        }
    });

这是错误:

    Error trying to execute js if (typeof(ca_weblite_codename1_js_JavascriptContext_LOOKUP_TABLE1) == 'undefined'){ca_weblite_codename1_js_JavascriptContext_LOOKUP_TABLE1=[]};(ca_weblite_codename1_js_JavascriptContext_RETURN_VAR_call_3=(ca_weblite_codename1_js_JavascriptContext_LOOKUP_TABLE1[0]).call(ca_weblite_codename1_js_JavascriptContext_LOOKUP_TABLE1[1], 'print', 'I', 'am', 'in', 'syso'))
[EDT] 0:0:0,2 - Codename One revisions: 0576ed169fa3bf9f9fe32b3ad516f3aeee60a66c

[EDT] 0:0:0,3 - Exception: java.lang.RuntimeException - Failed to execute javascript ca_weblite_codename1_js_JavascriptContext_RETURN_VAR_call_3=(ca_weblite_codename1_js_JavascriptContext_LOOKUP_TABLE1[0]).call(ca_weblite_codename1_js_JavascriptContext_LOOKUP_TABLE1[1], 'print', 'I', 'am', 'in', 'syso').  The error was netscape.javascript.JSException: TypeError: 'undefined' is not a function (evaluating '(ca_weblite_codename1_js_JavascriptContext_LOOKUP_TABLE1[0]).call(ca_weblite_codename1_js_JavascriptContext_LOOKUP_TABLE1[1], 'print', 'I', 'am', 'in', 'syso')')
java.lang.RuntimeException: Failed to execute javascript ca_weblite_codename1_js_JavascriptContext_RETURN_VAR_call_3=(ca_weblite_codename1_js_JavascriptContext_LOOKUP_TABLE1[0]).call(ca_weblite_codename1_js_JavascriptContext_LOOKUP_TABLE1[1], 'print', 'I', 'am', 'in', 'syso').  The error was netscape.javascript.JSException: TypeError: 'undefined' is not a function (evaluating '(ca_weblite_codename1_js_JavascriptContext_LOOKUP_TABLE1[0]).call(ca_weblite_codename1_js_JavascriptContext_LOOKUP_TABLE1[1], 'print', 'I', 'am', 'in', 'syso')')
    at com.codename1.javascript.JavascriptContext.call(JavascriptContext.java:975)
    at com.codename1.javascript.JavascriptContext.call(JavascriptContext.java:862)
    at com.codename1.javascript.JavascriptContext.call(JavascriptContext.java:807)
    at com.codename1.javascript.JSObject.call(JSObject.java:960)
    at net.etceterum.app.javascript.JSEngine.lambda$0(JSEngine.java:105)

         //JSEngine.java:105 = syso.call("print", "I", "am", "in", "syso");

at net.etceterum.app.javascript.JSEngine$$Lambda$9/30625616.actionPerformed(Unknown Source)
    at com.codename1.ui.util.EventDispatcher.fireActionSync(EventDispatcher.java:459)
    at com.codename1.ui.util.EventDispatcher.access$100(EventDispatcher.java:45)
    at com.codename1.ui.util.EventDispatcher$CallbackClass.run(EventDispatcher.java:95)
    at com.codename1.ui.Display.processSerialCalls(Display.java:1114)
    at com.codename1.ui.Display.edtLoopImpl(Display.java:1058)
    at com.codename1.ui.Display.mainEDTLoop(Display.java:946)
    at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120)
    at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)

我做错了什么?

感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:0)

问题是你正试图打电话给#34; syso&#34;作为一个功能,使用参数&#34; print&#34;,&#34; I&#34;,&#34; am&#34;,&#34; in&#34;,&#34; syso&#34; 。在JS中,你所做的是

"//*[@id=\"ctl00_ContentPlaceHolder1_ListViewNodos_ctrl0_ListViewArchivosSIN_ctrl0_linkCSV\"]"

更改

window.syso('print','I','am','in','syso');

syso.call("print", "I", "am", "in", "syso");

它会起作用。