所以这在javafx的例子中起作用,当时我的电脑有jdk 1.7.0,所以这可能是java8中FX的新版本;但
我得到了一个很好的堆栈跟踪
jfx-project-run:
Executing E:\workspace\PathFinderApp\dist\run1095471771\PathFinderApp.jar using platform C:\Program Files\Java\jdk1.8.0\jre/bin/java
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:367)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:305)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:894)
at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:56)
at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:158)
at java.lang.Thread.run(Thread.java:744)
Caused by: javafx.fxml.LoadException:
file:/E:/workspace/PathFinderApp/dist/run1095471771/PathFinderApp.jar!/com/rpg/gui/main.fxml:11
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2617)
at javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:104)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:937)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:976)
at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:216)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:738)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2723)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3230)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3191)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3164)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3140)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3120)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3113)
at com.rpg.gui.GUI.loadMainPane(GUI.java:34)
at com.rpg.gui.GUI.initialize(GUI.java:20)
at Main.start(Main.java:20)
at com.sun.javafx.application.LauncherImpl$8.run(LauncherImpl.java:837)
at com.sun.javafx.application.PlatformImpl$7.run(PlatformImpl.java:335)
at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:301)
at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:298)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$6.run(PlatformImpl.java:298)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$300(WinApplication.java:39)
at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112)
... 1 more
Caused by: java.lang.InstantiationException: com.rpg.gui.MainController
at java.lang.Class.newInstance(Class.java:418)
at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:932)
... 26 more
Caused by: java.lang.NoSuchMethodException: com.rpg.gui.MainController.<init>()
at java.lang.Class.getConstructor0(Class.java:2971)
at java.lang.Class.newInstance(Class.java:403)
... 28 more
Exception running application Main
Java Result: 1
基本上告诉我这里有些问题"com/rpg/gui/main.fxml:11"
,该行是
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml" fx:controller="com.rpg.gui.MainController">
所以我可以弄清楚它不喜欢与主控制器有关,所以它必须与加载fxml和控制器的权利有关吗?但这就是我的想法结束的地方
MainController mainController = new MainController(path);
Pane mainPane = FXMLLoader.load(getClass().getResource("main.fxml"));
Window.setMainController(mainController);
Window.swap(path+"content.fxml");
这是在javaFX中有经验的任何人抛出一切的方法,或者知道java8中的任何变化都会这样做吗?
答案 0 :(得分:17)
您的MainController
没有零参数构造函数。如果FXMLLoader
在根元素上遇到fx:controller
属性,它会尝试通过(有效地)调用属性中指定的类的零参数构造函数来创建该控制器的实例。
要解决此问题(最简单的方法),从FXML文件中删除fx:controller
属性,然后手动设置控制器&#34;&#34;在FXMLLoader
上。您需要创建FXMLLoader
实例,而不是依赖静态load(...)
方法:
FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml"));
loader.setController(new MainController(path));
Pane mainPane = loader.load();
答案 1 :(得分:4)
我遇到了同样的问题,希望与之分享一些相关内容。我正在使用java 8和Netbeans 8.1,当我创建一个javafx FXML应用程序时,我得到了这个 以下是一些提示:
您可以在fxml
文件或main
类中定义控制器。
如果要在main
类中定义控制器,请使用 @James_D 描述的方法。如果要在fxml
文件中定义,而不是使用fx:controller
属性
fx:controller="yourProjectName.yourFXMLDocumentControllerName"
并在主类中将其引用为
Parent root = FXMLLoader.load(getClass().getResource("yourFXMLFileName.fxml"));
希望它会对某人有所帮助。
答案 2 :(得分:2)
我也遇到了很多......在Scenebuilder / netbeans 8中似乎有一个错误,在Scenebuilder中保存后,它会创建另一个xmlns:fx =“...”,这可能是一个问题..
另外,看看我使用java8的fxml,我有这些: xmlns =“http://javafx.com/javafx/8”xmlns:fx =“http://javafx.com/fxml/1”
我注意到你的是: 的xmlns:FX = “http://javafx.com/fxml”
答案 3 :(得分:2)
您使用了
Pane mainPane = FXMLLoader.load(getClass().getResource("main.fxml"));
尝试
Pane mainPane = FXMLLoader.load(getClass().getResource("/main.fxml"));
答案 4 :(得分:2)
对于那些在JDK11 / JavaFX11上苦苦挣扎的人
我收到此错误的原因是因为该项目依赖于 一个UI模块,其中包含FXML引用以及图形配置。 我在下面的编译日志转储中注意到了这些行:
产生于:java.lang.IllegalAccessException: com.sun.javafx.application.LauncherImpl类(在javafx.graphics模块中)无法访问 class academy.learnprogramming.ui.Main(在academy.learnprogramming.common模块中) 因为模块academy.learnprogramming.common 不会将academy.learnprogramming.ui导出到模块javafx.graphics
所以
假设这不是一个Maven项目, Project / module-name / src / module-info.java ,您需要确保模块系统可以访问项目中的所有模块:< / p>
module com.test.common {
requires javafx.fxml;
requires javafx.controls;
requires javafx.graphics;
requires java.sql;
// ... etc.
opens com.test.common;
opens com.test.ui; // For my particular case, I had Forgotten this one
opens com.test.db; // And this one
}
参考:
Exception in Application constructor
Exception in thread "main" java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class academy.learnprogramming.ui.Main
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:890)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.IllegalAccessException: class com.sun.javafx.application.LauncherImpl (in module javafx.graphics) cannot access class academy.learnprogramming.ui.Main (in module academy.learnprogramming.common) because module academy.learnprogramming.common does not export academy.learnprogramming.ui to module javafx.graphics
at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:361)
at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:591)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:802)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
... 1 more
答案 5 :(得分:1)
在运行>>编辑配置
将此行添加到“ VM选项”:
--module-path /path/to/JavaFX/lib --add-modules=javafx.controls,javafx.fxml
这对我来说是搜索音色
答案 6 :(得分:0)
如果任何人都无法通过以下答案解决问题,则简单的方法是创建新的fxml空类,然后单击导致问题的fxml的编辑,并复制xml代码转到新课程。 转到start()方法,更改:
Pane mainPane = FXMLLoader.load(getClass().getResource("newClass.fxml"));
到
int num1 = 30;
int num2 = 20;
int num_random = rand()%num1;
while (num_random < num2){
num_random = rand()%30;
}
答案 7 :(得分:0)
layout.getChildren().addAll(choiceBox, button);
我试图在布局中添加按钮,但是我没有定义按钮对象,它看起来像这样:
Button button;
当我这样做时:
button = new Button("Click me");
一切正常。