移动设备上的 FXML 脚本事件处理程序支持

FXML Script Event Handlers support on mobile

我想在 javafx 应用程序的 fxml 文件中使用脚本化事件处理程序,这应该 运行 在移动设备上。
我正在使用版本 2 的 jfxmobile-plugin 为移动设备交叉编译,它使用 gluonvm:

org.javafxports:jfxmobile-plugin:2.0.29

javafxports 一般支持 iOS and/or Android 上的 FXML 脚本事件处理程序吗?

我正在尝试在 Android 和 iOS 上跟踪可在桌面上运行的 fxml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<?language javascript?>
...
<Button fx:id="button" onMouseClicked="label.setText('Button has been clicked from JavaScript');" text="%button.text" />

在 Android 和 iOS 我得到以下异常:

System.err(19642): Exception in Application start method
System.out(19642): QuantumRenderer: shutdown
System.err(19642): java.lang.reflect.InvocationTargetException
System.err(19642):  at java.lang.reflect.Method.invoke(Native Method)
System.err(19642):  at java.lang.reflect.Method.invoke(Method.java:372)
System.err(19642):  at javafxports.android.DalvikLauncher.run(DalvikLauncher.java:188)
System.err(19642):  at java.lang.Thread.run(Thread.java:818)
System.err(19642): Caused by: java.lang.RuntimeException: Exception in Application start method
[MALI][Gralloc](193): [+]hnd:0x7fb884c000, fd:27, ion_hnd(0x4), req_format(0x1), int_fmt(0x1)  byte_stride(3200), flags(0x4), usage(0xb00), size(3788800), alloc_size(3788800) 800(800)X1184(1184) pid(193)  sec (0)
System.err(19642):  at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
System.err(19642):  at com.sun.javafx.application.LauncherImpl.lambda$launchApplication8(LauncherImpl.java:182)
System.err(19642):  at com.sun.javafx.application.LauncherImpl.access$lambda(LauncherImpl.java)
System.err(19642):  at com.sun.javafx.application.LauncherImpl$$Lambda.run(Unknown Source)
System.err(19642):  ... 1 more
System.err(19642): Caused by: javafx.fxml.LoadException: Error resolving onMouseClicked='label.setText('Button has been clicked from JavaScript');', either the event handler is not in the Namespace or there is an error in the script.
System.err(19642): file:/data/app/com.indsp.software.playground.javafxmobiledemo.demoapp-1/base.apk!/com/indsp/software/playground/javafxmobiledemo/demoapp/scenes/Scene1.fxml:46
HAL(19642): loaded HAL id=gralloc path=/system/lib/hw/gralloc.mt8163.mali.so hmi=0x0 handle=0xf46f9154
[MALI][Gralloc](19642): [+]r_hnd:0xf7a2e728, fd:44, ion_hnd(0x4), req_format(0x1), int_fmt(0x1)  byte_stride(3200), flags(0x4), usage(0xb00), size(3788800), alloc_size(3788800) 800(800)X1184(1184) pid(19642)  sec (0)
Kernel(193): [371573.005712] <2> (2)[236:Binder_1][name:ion_mm_heap&][ION][ion_dbg] alloc_pages order=2 cache=0
System.err(19642):  at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
System.err(19642):  at javafx.fxml.FXMLLoader.access0(FXMLLoader.java:103)
System.err(19642):  at javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:610)
System.err(19642):  at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:770)
System.err(19642):  at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823)
System.err(19642):  at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532)
System.err(19642):  at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
System.err(19642):  at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
System.err(19642):  at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
System.err(19642):  at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
System.err(19642):  at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
System.err(19642):  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3116)
System.err(19642):  at com.indsp.software.playground.javafxmobiledemo.demoapp.DemoApp.start(DemoApp.java:21)
System.err(19642):  at com.sun.javafx.application.LauncherImpl.lambda$launchApplication15(LauncherImpl.java:863)
System.err(19642):  at com.sun.javafx.application.LauncherImpl.access$lambda(LauncherImpl.java)
System.err(19642):  at com.sun.javafx.application.LauncherImpl$$Lambda.run(Unknown Source)
System.err(19642):  at com.sun.javafx.application.PlatformImpl.lambda$runAndWait8(PlatformImpl.java:326)
System.err(19642):  at com.sun.javafx.application.PlatformImpl.access$lambda(PlatformImpl.java)
System.err(19642):  at com.sun.javafx.application.PlatformImpl$$Lambda.run(Unknown Source)
System.err(19642):  at com.sun.javafx.application.PlatformImpl.lambda$null6(PlatformImpl.java:295)
System.err(19642):  at com.sun.javafx.application.PlatformImpl.access$lambda(PlatformImpl.java)
System.err(19642):  at com.sun.javafx.application.PlatformImpl$$Lambda.run(Unknown Source)
System.err(19642):  at java.security.AccessController.doPrivileged(AccessController.java:52)
System.err(19642):  at com.sun.javafx.application.PlatformImpl.lambda$runLater7(PlatformImpl.java:294)
System.err(19642):  at com.sun.javafx.application.PlatformImpl.access$lambda(PlatformImpl.java)
System.err(19642):  at com.sun.javafx.application.PlatformImpl$$Lambda.run(Unknown Source)
System.err(19642):  at com.sun.glass.ui.monocle.RunnableProcessor.runLoop(RunnableProcessor.java:93)
System.err(19642):  at com.sun.glass.ui.monocle.RunnableProcessor.run(RunnableProcessor.java:52)
System.err(19642):  ... 1 more

根据您得到的错误:

...either the event handler is not in the Namespace or there is an error in the script.

您可以在 FXMLLoader class 查找打印的消息,您会发现:

if (handlerName.length() == 0 || scriptEngine == null) {
    throw constructLoadException("Error resolving " + attribute.name + "='" + attribute.value
         + "', either the event handler is not in the Namespace or there is an error in the script.");
}

eventHandler = new ScriptEventHandler(handlerName, scriptEngine);

所以看起来似乎没有 scriptEngine

要确认这一点,您可以尝试 运行 在桌面和移动设备上进行简单测试:

ScriptEngineManager manager = new ScriptEngineManager();
if (manager != null) {
    for (ScriptEngineFactory factory : manager.getEngineFactories()) {
        System.out.println("factory: " + factory.getEngineName() + " " + factory.getLanguageName() + " " + factory.getExtensions());
    }
}

在桌面上打印:

Oracle Nashorn ECMAScript [js]

在移动设备上(Android 和 iOS),它不打印任何内容。

因此这证实了移动设备不支持脚本事件处理程序。

就个人而言,我不喜欢在桌面上使用它们。