我通过Gluon客户端插件运行由GraalVM(基于JDK 11构建的最新GraalVM)生成的本机映像时遇到错误。
javafx.fxml.LoadException:解决onAction ='#loginAction'时出错,事件处理程序不在命名空间中或脚本中有错误。fxml / LoginScreen.fxml:17
整理步骤可以正常工作:
mvn clean client:build
我在名为“ projectname / target / client / x86_64-linux / binaryname ”的文件夹中看到了二进制文件
当我通过“ ./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事件的名称与控制器(面向初学者的JavaFX简介)-第27页中指定的名称不同。但是事实并非如此,我的程序的命名是准确的。使用JavaFX maven插件(与GluonClient分开),使用
Maven的JavaFX:运行
该程序可以正确启动并按预期工作。如果我需要发布更多信息,请告诉我。
这是我的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();
如果您查看HelloFXML
“客户端示例”存储库中的示例,您将看到它使用带有控制器的典型FXML文件:
<AnchorPane fx:id="pane" ... fx:controller="hellofx.HelloController">
在您的情况下,您没有FXML文件中的控制器,但是您可以像这样提供它:
fxmlLoader.setController(new hellofx.HelloController());
众所周知,FXMLLoader
使用反射实例化在解析FXML文件时找到的控制器,控件和方法。
无论哪种方式,当您单击触发该loginAction
方法的按钮时,FXMLLoader都会通过以下调用进行处理:
MethodHelper.invoke(method, controller, params);
使用反射来处理此类事件。
对于GraalVM,反射是一个问题,您必须通过提供将在某些时候反射使用的类/方法/字段来“帮助”一点。在这里找到更多关于它的信息。
客户端插件已经为您提供了添加JavaFX核心类和方法的服务。您可以看到中添加了什么target/client/x86_64-darwin/gvm/reflectionconfig-x86_64-darwin.json
。
但是,必须将自定义类添加到该文件。有两种方法可以做到这一点:
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
}
...
这应该足以解决您的问题。
reflectionconfig.json
)META-INF/substrate/config
代替:[
{
"name":"hellofx.HelloController",
"methods":[{"name":"loginAction","parameterTypes":["javafx.event.ActionEvent"] }]
}
]
这也将修复它。当然,它可能也需要添加控制器中其他的方法(例如initialize
)。
这将仅对这种方法开放,因此它对内存占用的影响较小,并且遵循该插件对JavaFX核心类的作用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句