尝试运行TornadoFX应用程序时发生java.lang.NoSuchMethodException

不知道是什么导致它没有找到我的视图上的“初始化”功能,所以我想我会在这里发布,看看是否有其他人有这个问题。

一切都编译好! 然后当我运行我的程序时,我得到这个错误:

java.lang.InstantiationException: com.my.tfx.app.InputView at java.lang.Class.newInstance(Class.java:427) at tornadofx.FXKt.find(FX.kt:372) at tornadofx.FXKt.find$default(FX.kt:358) at tornadofx.App.start(App.kt:80) at com.my.tfx.app.UserInputUI.start(UserInputUI.kt:15) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863) at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326) at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294) at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method) at com.sun.glass.ui.gtk.GtkApplication.lambda$null$5(GtkApplication.java:139) at java.lang.Thread.run(Thread.java:745) Caused by: java.lang.NoSuchMethodException: com.my.tfx.app.InputView.<init>() at java.lang.Class.getConstructor0(Class.java:3082) at java.lang.Class.newInstance(Class.java:412) ... 13 more 

不太清楚是什么导致这个..我有这样的设置:

 class UserInputUI : App(InputView(SVGEnum.first, StringEnum.first, UserInput.validationFunctions)::class, UIStyles::class) { init { reloadStylesheetsOnFocus() } override fun start(stage: Stage) { super.start(stage) stage.minWidth = 1024.0 stage.minHeight = 768.0 stage.maxWidth = 2560.0 stage.maxHeight = 1440.0 } } class InputView(val s: SVGEnum, val q: StringEnum, val valFunArray : ArrayList<(String)-> Boolean>) : View() { override val root = stackpane { //contents ommitted cause they're super long and I dont think its relevant, //but I can add it in if requested } } 

有任何想法吗? 或者这是一个错误? 谢谢!

视图必须有一个没有参数的构造函数,以便它们可以被框架实例化。 在您的应用程序子类( UserInputUI )中,您实际上实例化InputView InputView,然后调用::class来获取它的KClass。 你只能直接将它传递给KClass,所以你需要修改你的代码,这样UserInputUI就是这样定义的:

 class UserInputUI : App(InputView::class, UIStyles::class) 

(我已经省略了init块和启动覆盖,顺便说一下,确保在生产reloadStylesheetsOnFocus中不要调用reloadStylesheetsOnFocus ,为了确保从不进行生产,请将其删除并在TornadoFX IDEA运行配置中设置选项)。

接下来,您必须确保InputView具有noargs构造函数。 您需要使用其他技术将参数传递给它。 既然你在你的App类中硬编码,你可能直接在InputView直接编写它们,或者你可以引入一个你在App.start配置的ViewModel ,如果你喜欢,你可以根据命令行参数或配置文件。

InputView而不是UserInputIU硬编码值的重写将如下所示:

 class InputView() : View() { val s: SVGEnum = SVGEnum.first val q: StringEnum = StringEnum.first val valFunArray: ArrayList<(String) -> Boolean> = UserInput.validationFunctions override val root = stackpane { } } 

我希望澄清这个问题:)