代码之家  ›  专栏  ›  技术社区  ›  David Phan

Javafx加载xml文件时出错

  •  0
  • David Phan  · 技术社区  · 7 年前

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

    sample fxml loaded

    但不是startScreen或MainView fxml文件

    MainView not being able to load

    调用TargetException RuntimeException:应用程序启动方法中的异常 javafx.fxml.LoadException ClassNotFoundException:view.MainView

    重建程序并不能解决问题。 我看到一些网站提到一些关于资源文件没有被制作?可能是这样?如果是这样的话,有人能对它有更多的了解吗。

    代码如下:

    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$launchApplication$155(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.access$700(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$launchApplication1$162(LauncherImpl.java:8   63)
    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.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
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   tomtzook    7 年前

    根据stacktrace,加载时发生异常 mainView.fxml 是由该文件第12行中的某些内容引起的。如果你能把文件的内容贴出来,我可以回答得更清楚。

    这可能是一个评论,但我是新来的,没有足够的声誉…抱歉。