线程“主”中的异常java.lang.reflect.InvocationTargetException JavaFX(首先由显示图像和播放音频文件引起)

时间:2019-12-28 05:08:13

标签: javafx resources invocationtargetexception

在你们给我另一个链接之前,我已经研究了我的案子两天,没有运气:(

  

我的问题是如何在不获取错误消息的情况下声明ImageView / AudioClip / MediaPlayer

首先,让我向您显示错误消息:

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:567)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)

Caused by: java.lang.ExceptionInInitializerError
    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:567)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    ... 5 more

Caused by: java.lang.IllegalStateException: Not on FX application thread; currentThread = main
    at javafx.graphics/com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:291)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:424)
    at javafx.graphics/javafx.stage.Stage.<init>(Stage.java:254)
    at javafx.graphics/javafx.stage.Stage.<init>(Stage.java:240)
    at YuanGeometryWar/Output.Main.<clinit>(Main.java:18)
    ... 11 more
  

我知道此问题是由于图像声明引起的:

现在的代码行如何(给我错误):

URL background = this.getClass().getResource("resources/Image/MenuBackGround.jpg");
ImageView menuBack = new ImageView(new Image(background.toString()));

mainPane.getChildren().add(menuBack);

以前如何(类似于此并且可以正常工作):

try(InputStream pathIntro = Files.newInputStream(Paths.get("resources/Image/ControlBack.png"))){
        intro = new ImageView(new Image(pathIntro));

        controlPane.getChildren().add(intro);
    }

在使audioClip或MediaPlayer正常工作时,我也遇到同样的问题。

  

我只是想知道为什么这会导致我出现问题,以及实现这种目标的正确方法是什么?

The resources folder is not in src and is directly under the project folder

0 个答案:

没有答案