Javafx 加载 xml 文件奇怪错误

Javafx Loading xml Files Weird Error

我使用 IDEA Intellij。

我遇到一个问题,我可以加载某些 xml 文件,但其他文件不能。

例如,我可以加载 gameFrame 并采样 fxml 个文件。

sample fxml loaded

但不是 startScreen 或 MainView fxml 个文件

MainView not being able to load

弹出的一些例外情况是: 调用目标异常 RuntimeException:应用程序启动方法中的异常 javafx.fxml.LoadException ClassNotFoundException:view.MainView

当我单击问题的根源时,它会将我引导至:startLayout = loader.load();行,当我在启动方法中调用它时。

重建程序无法解决问题。 我看到一些网站提到没有制作资源文件?可能是这样吗?如果是这样,有人可以对此有更多的了解。

代码如下:

package sample;

 import javafx.application.Application;
 import javafx.fxml.FXMLLoader;
 import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import java.io.IOException;

public class Main extends Application {

private Pane startLayout, gameFrame;
private Stage primaryStage;

//
@Override
public void start(Stage primaryStage) throws Exception {
    this.primaryStage = primaryStage;
    this.primaryStage.setTitle("Ron");

    showStartScreen();
}
//

public void showStartScreen() throws IOException {

    FXMLLoader loader = new FXMLLoader();
    loader.setLocation(Main.class.getResource("/view   /mainView.fxml"));
    startLayout = loader.load();
    Scene scene = new Scene(startLayout);
    primaryStage.setScene(scene);
    primaryStage.show();


}


public static void main(String[] args) {
    launch(args);
}
}

错误信息:

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$launchApplication5(LauncherImpl.java:18   2)
at java.lang.Thread.run(Thread.java:748)
Caused by: javafx.fxml.LoadException: 
/C:/Users/Quy%20Phan/IdeaProjects/Ronv2/out/production/Ronv2/view/mainView.fxml:12

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.access0(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:922)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at sample.Main.showStartScreen(Main.java:42)
at sample.Main.start(Main.java:34)
at      com.sun.javafx.application.LauncherImpl.lambda$launchApplication12(LauncherImpl.java:8   63)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait5(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null3(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater4(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$null8(WinApplication.java:191)
... 1 more
Caused by: java.lang.ClassNotFoundException: view.MainView
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:920)
... 18 more
Exception running application sample.Main

Process finished with exit code 1

根据您的堆栈跟踪,异常发生在加载 mainView.fxml 时,并且是由该文件第 12 行中的内容引起的。如果你能post文件的内容我可以回答得更清楚。

这应该是一条评论,但我是新来的,没有足够的声誉...抱歉。