线程“main”中的异常java.lang.RuntimeException:无法构造Application实例:class View

时间:2015-07-01 23:54:52

标签: java exception javafx runtime

我正在尝试在我编写静态main方法之外实例化Application类型的对象,并且我得到了这个异常。

public class Main {
    public static void main(String[] args) {
        new View(args);
    }
}
import javafx.application.Application;

public class View extends Application {
    public View(String... args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
    }
}

堆栈跟踪:

Exception in Application constructor
Exception in thread "main" java.lang.RuntimeException: Unable to construct Application instance: class View
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:907)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(LauncherImpl.java:182)
    at com.sun.javafx.application.LauncherImpl$$Lambda$2/1867083167.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$158(LauncherImpl.java:819)
    at com.sun.javafx.application.LauncherImpl$$Lambda$46/1861073381.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl$$Lambda$48/1540794519.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
    at com.sun.javafx.application.PlatformImpl$$Lambda$50/1604144171.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
    at com.sun.javafx.application.PlatformImpl$$Lambda$49/718368050.run(Unknown Source)
    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.lambda$null$145(WinApplication.java:101)
    at com.sun.glass.ui.win.WinApplication$$Lambda$38/1823101961.run(Unknown Source)
    ... 1 more
Caused by: java.lang.IllegalStateException: Application launch must not be called more than once
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:162)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:143)
    at javafx.application.Application.launch(Application.java:252)
    at View.<init>(View.java:33)
    ... 18 more

1 个答案:

答案 0 :(得分:1)

不确定您要做什么,但在构造函数中调用launch(args)看起来不对。来自javadocs(https://docs.oracle.com/javase/8/javafx/api/javafx/application/Application.html#launch-java.lang.String...-):

  

在应用程序退出之前,通过调用Platform.exit或所有应用程序窗口都已关闭,启动方法不会返回。

即使它有效,它也会挂在你的构造函数中。如果需要在main()方法之外进行,请使用一些静态实例化器。