javafx - 在 JavaFX 中出现闪屏后尝试切换阶段
问题描述
我正在尝试显示启动画面,直到我加载所有必要的资源并随后打开主舞台,但我一直遇到InvocationTargetException
.
换句话说,我的初级阶段加载了一个 FXML,它有一个如下所示的控制器:
public class SplashController {
@FXML
VBox splashScreenVBox = new VBox();
@FXML
protected void initialize() throws InterruptedException, IOException {
Stage primaryStage = (Stage) splashScreenVBox.getScene().getWindow();
primaryStage.close();
new MainStage();
}
}
该类MainStage
只是加载 FXML 并显示场景:
public MainStage() throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("/core/views/Main.fxml"));
this.setScene(new Scene(root, 800, 600));
this.show();
}
我得到的错误指向我FXMLLoader.load()
第一次使用 FXML 的行(我有两个 FXML 文件,每个阶段一个)。
有人可以解释一下为什么会发生这种情况,最好是如何正确使用FXMLLoader
,以防我正在做的事情是一个问题,好吗?
编辑:堆栈跟踪
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException:
/C:/Users/REDACTED/out/production/REDACTED/core/views/Splash.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2571)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at core.Scenes.start(Scenes.java:19)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
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$148(WinApplication.java:191)
... 1 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2566)
... 17 more
Caused by: java.lang.NullPointerException
at core.controllers.SplashController.initialize(SplashController.java:18)
... 28 more
Exception running application core.Scenes
解决方案
正如您从堆栈跟踪中看到的那样,您在 on lineNullPointerException
的initialize
方法中得到了一个。我将假设该行是这一行:SplashController
18
18
Stage primaryStage = (Stage) splashScreenVBox.getScene().getWindow();
问题很可能是这个电话:getScene().getWindow()
. 该方法getScene()
将返回null
此处,因为splashScreenVBox
它还不是 a 的一部分Scene
。怎么会这样?该initialize
方法在执行期间FXMLLoader.load()
被调用。这意味着您还没有机会将结果添加FXMLLoader.load()
到 a中Scene
。
要解决这个问题,一个选项是向您添加一个方法来SplashController
加载MainStage
.
FXMLLoader loader = new FXMLLoader(getClass().getResource("your/resource"));
Parent root = loader.load();
Stage splashStage = new Stage();
splashStage.setScene(new Scene(root));
splashStage.show();
SplashController controller = loader.getController();
controller.loadMainApp(splashStage);
我将 传递splashStage
给该方法,因此您可以在准备显示 main 时隐藏/关闭它Stage
。根据您的代码设计,除了传递参数之外,可能还有其他方法可以做到这一点。
一个如何创建一个抽象控制器的示例,该控制器将在根添加到 aScene
并且Scene
已添加到 a时自动调用方法Window
。
import java.util.function.Consumer;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.stage.Window;
public abstract class AbstractController<T extends Node> {
// protected so subclasses can access the root
// directly. You could also hide this behind a
// getter.
@FXML protected T root;
// Subclasses that override this method must call the
// super implementation
@FXML
protected void initialize() {
Consumer<Window> onNewWindow = this::onAddedToWindow;
Consumer<Scene> onNewScene = scene ->
scene.windowProperty().addListener(new SelfRemovingChangeListener<>(onNewWindow));
root.sceneProperty().addListener(new SelfRemovingChangeListener<>(onNewScene));
}
protected abstract void onAddedToWindow(Window window);
private static class SelfRemovingChangeListener<T> implements ChangeListener<T> {
private final Consumer<? super T> onNewValue;
private SelfRemovingChangeListener(Consumer<? super T> onNewValue) {
this.onNewValue = onNewValue;
}
@Override
public void changed(ObservableValue<? extends T> observable, T oldValue, T newValue) {
onNewValue.accept(newValue);
observable.removeListener(this);
}
}
}
这要求fx:id="root"
您的 FXML 文件中有一个 并且根对象是一个Node
. 仅在第一次添加到aonAddedToWindow
时才调用。您仍然必须将 添加到其他地方(无论您打电话到哪里)。root
Window
root
Window
FXMLLoader.load
推荐阅读
- mongodb - 更改配置文件后是否需要重新启动 Mongo?
- ffmpeg - H.264 到 DNxHR 444 问题。颜色未正确转码(HDR 项目)。注:问题尚未解决
- random - 遗传算法中染色体缺失属性
- javascript - 如何返回嵌套数组中的唯一项
- css - 使用 Emotion css 将图像添加到按钮
- javascript - 我不明白为什么气球在我制作的小游戏中没有移动
- javascript - 简单的javascript函数返回一个未定义的字符串
- c++ - std::bind 是否实现了 std::ref 和 std::cref 以消除函数调用的歧义?
- python - AZure DataBricks - 大型 CSV 文件字段验证
- javascript - 使用 DataTables ajax 响应中的变量来填充 yadcf 过滤器