GraalVM javafx.fxml.LoadException:解决onAction ='#loginAction'时出错,事件处理程序不在命名空间中

我在通过Gluon客户端插件运行GraalVM(基于JDK 11构建的最新GraalVM)产生的本机映像时遇到错误。

javafx.fxml.LoadException:解决onaction ='#loginaction'时出错,事件处理程序不在命名空间中或脚本中有错误。 fxml / LoginScreen.fxml:17

整理步骤可以正常工作:

mvn clean client:build

我在名为“ 项目名称 / target / client / x86_64-linux / 二进制名称”的文件夹中看到了二进制文件

当我通过“ ./ binaryname ”运行可执行文件时,产生了以上错误

第17行抱怨的FXML代码行是:

<Button fx:id="_loginButton" layoutX="516.0" layoutY="174.0" mnemonicParsing="false" onaction="#loginaction" prefHeight="28.0" prefWidth="94.0" text="Login" />

支持代码逻辑如下,并标记为@FXML:

@FXML
void loginaction(actionEvent event) throws InterruptedException {

    LoginService loginservice = new LoginService(_usernameTextField.getText(),_passwordTextField.getText());

根据JavaFX的常见错误列表,该问题通常是因为onaction事件的名称与控制器中指定的名称不同-Introduction to JavaFX for Beginner Programmers - Pg 27 。但是事实并非如此,我的程序的命名是准确的。使用

使用JavaFX maven插件(与GluonClient分开)

maven javafx:run

该程序正确启动并按预期运行。如果我需要发布更多信息,请告诉我。

这是我的pom.xml(我仅在下面替换了我的包的名称)

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo</groupId>
<artifactId>com-demo-management-ui</artifactId>
<version>0.0.1-snAPSHOT</version>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
    <client.plugin.version>0.1.26</client.plugin.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-controls</artifactId>
        <version>11</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.openjfx/javafx-fxml -->
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-fxml</artifactId>
        <version>11</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.12</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.6</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.6</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <release>11</release>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-maven-plugin</artifactId>
            <version>0.0.3</version>
            <configuration>
                <mainClass>com.demo.Launcher</mainClass>
            </configuration>
        </plugin>
        <plugin>
            <groupId>com.gluonhq</groupId>
            <artifactId>client-maven-plugin</artifactId>
            <version>${client.plugin.version}</version>
            <configuration>
                <!-- Uncomment to run on iOS: -->
                <!-- <target>ios</target> -->
                <mainClass>com.demo.Launcher</mainClass>
                <graalvmHome>/opt/graalvm-ce-java11-20.2.0-dev/</graalvmHome>
            </configuration>
        </plugin>
    </plugins>
</build>

<pluginRepositories>
    <pluginRepository>
        <id>gluon-releases</id>
        <url>http://nexus.gluonhq.com/nexus/content/repositories/releases/</url>
    </pluginRepository>
</pluginRepositories>

最后,这是我设置控制器的代码(这是一个方法调用,我根据需要交换视图,因此控制器在创建视图时作为参数传递):

        FXMLLoader fxmlLoader = new FXMLLoader(getclass().getResource("/fxml/"+baseController.getFxmlFile()));
        fxmlLoader.setController(baseController);
        Parent parent =  fxmlLoader.load();
        Scene scene = new Scene(parent);
        Stage stage = new Stage();
        stage.setfullScreen(fullScreen);
        stage.setMaximized(setMaximized);
        stage.setScene(scene);
        stage.show();
iCMS 回答:GraalVM javafx.fxml.LoadException:解决onAction ='#loginAction'时出错,事件处理程序不在命名空间中

如果您查看Client samples repository中的HelloFXML示例,您会看到它使用带有控制器的典型FXML文件:

<AnchorPane fx:id="pane" ... fx:controller="hellofx.HelloController">

在您的情况下,您在FXML文件中没有控制器,但是您可以像这样提供它:

fxmlLoader.setController(new hellofx.HelloController());

您知道,FXMLLoader使用反射来实例化在解析FXML文件时发现的控制器,控件和方法。

无论哪种方式,当您单击触发loginAction方法的按钮时,使用此call的FXMLLoader进程:

MethodHelper.invoke(method,controller,params);

使用反射来处理此类事件。

对于GraalVM, reflection 是个问题,您必须通过提供在某些时候将被反射使用的类/方法/字段来“帮助”一点。进一步了解here

客户端插件已经为您添加JavaFX核心类和方法提供了帮助。您可以看到target/client/x86_64-darwin/gvm/reflectionconfig-x86_64-darwin.json中添加了什么。

但是,必须将自定义类添加到该文件。有两种方法可以做到:

  • 与HelloFXML中一样,通过配置/ reflectionList,您将提供将自定义使用的自定义类:
<plugin>
    <groupId>com.gluonhq</groupId>
    <artifactId>client-maven-plugin</artifactId>
    <version>${client.plugin.version}</version>
    <configuration>
          <reflectionList>
               <list>hellofx.HelloController</list>   <!-- your custom classes -->
          </reflectionList>
          <mainClass>${mainClassName}</mainClass>
    </configuration>
</plugin>

这具有打开所有类方法/字段进行反射的作用。您将在json文件中看到以下结果:

  {
    "name" : "hellofx.HelloController","allDeclaredConstructors" : true,"allPublicConstructors" : true,"allDeclaredFields" : true,"allPublicFields" : true,"allDeclaredMethods" : true,"allPublicMethods" : true
  }
...

这足以解决您的问题。

  • 通过配置文件。如您在客户端的documentation中所读,您可以向reflectionconfig.json上添加配置文件(META-INF/substrate/config):
[
  {
    "name":"hellofx.HelloController","methods":[{"name":"loginAction","parameterTypes":["javafx.event.ActionEvent"] }]
  }
]

这也将修复它。当然,这可能还需要添加控制器中具有的其他方法(例如initialize)。

这将仅对这种方法开放,因此它对内存占用的影响较小,并遵循该插件对JavaFX核心类的作用。

本文链接:https://www.f2er.com/2250723.html

大家都在问