0

不知道是什么导致它在我的视图上找不到“init”函数,所以我想我会在这里发帖,看看其他人是否有这个问题。

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

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
  }

}

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

4

1 回答 1

4

视图必须有一个无参数的构造函数,以便它们可以被框架实例化。在您的应用程序子类 ( UserInputUI) 中,您实际上实例化了您InputView自己,然后调用::class以获取它的 KClass。您只应该直接将 KClass 传递给它,因此您需要修改代码以便UserInputUI定义如下:

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

(我省略了初始化块和启动覆盖。顺便说一句,确保你没有reloadStylesheetsOnFocus在生产中调用。为了确保永远不会进入生产,删除它并在 TornadoFX IDEA 运行配置中设置选项) .

接下来,您必须确保它InputView有一个 noargs 构造函数。您需要使用另一种技术将参数传递给它。由于您在 App 类中对它们进行了硬编码,因此您也可以直接在其中进行硬编码InputView,或者您可以根据需要引入基于命令行参数或配置文件的配置ViewModelApp.start

一个简单地硬编码值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 {
    }
}

我希望能澄清这个问题:)

于 2017-06-24T20:04:38.447 回答