线程 "main" java.lang.RuntimeException 中的异常:无法构造应用程序实例:class 查看

Exception in thread "main" java.lang.RuntimeException: Unable to construct Application instance: class View

我试图在我编写静态 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$launchApplication2(LauncherImpl.java:182)
    at com.sun.javafx.application.LauncherImpl$$Lambda/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$launchApplication18(LauncherImpl.java:819)
    at com.sun.javafx.application.LauncherImpl$$Lambda/1861073381.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait2(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl$$Lambda/1540794519.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null0(PlatformImpl.java:295)
    at com.sun.javafx.application.PlatformImpl$$Lambda/1604144171.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater1(PlatformImpl.java:294)
    at com.sun.javafx.application.PlatformImpl$$Lambda/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$null5(WinApplication.java:101)
    at com.sun.glass.ui.win.WinApplication$$Lambda/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

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

The launch method does not return until the application has exited, either via a call to Platform.exit or all of the application windows have been closed.

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