2017-10-13 65 views
3

我細讀了該主題上的其他線程,但仍然無法讓我的代碼工作,因此尋求幫助。無法從JS執行我的java方法

下面是代碼:

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) 

我在做什麼錯?

感謝您提供的任何幫助。

+0

Steve可能更有資格回答這個問題,但是在我看來,像'syso'可能在此階段未定義 –

回答

0

問題是,您試圖通過參數「print」,「I」,「am」,「in」,「syso」將函數調用爲「syso」。在JS,你做了什麼是

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

變化

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

syso.call("print", new Object[]{"I", "am", "in", "syso"}); 

,它會工作。

+0

Hello Steve,感謝您的幫助。現在運行順利。這是光滑的,我沒有看到實際發生的任何事情。一,我沒有看到輸出中的任何東西,因爲syso應該system.out參數和兩個,我的調試器不會停止在System.out.println行執行時。我真的好奇... – ebardet

+0

Raaaahhh ...對不起。我亂碼了我的代碼。它如你所說的那樣工作。非常感謝。 – ebardet