我細讀了該主題上的其他線程,但仍然無法讓我的代碼工作,因此尋求幫助。無法從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)
我在做什麼錯?
感謝您提供的任何幫助。
Steve可能更有資格回答這個問題,但是在我看來,像'syso'可能在此階段未定義 –