【问题标题】:Javafx Loading xml Files Weird ErrorJavafx 加载 xml 文件奇怪的错误
【发布时间】:2017-07-10 20:27:47
【问题描述】:

我使用 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();行以及当我在 start 方法中调用它时。

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

代码如下:

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

【问题讨论】:

  • 发布完整的堆栈跟踪。

标签: xml javafx resources filenotfoundexception scenebuilder


【解决方案1】:

根据您的堆栈跟踪,加载mainView.fxml 时发生异常,是由该文件第 12 行中的某些内容引起的。如果您可以发布文件的内容,我可以更清楚地回答。

这可能应该是一个评论,但我是新来的,没有足够的声誉......对不起。

【讨论】:

  • javafx.com/javafx" xmlns:fx="javafx.com/fxml" fx:controller ="view.MainView" prefHeight="400.0" prefWidth="600.0">
  • 在view.MainView中MainView类高亮为红色
  • 哦!我想我现在已经想通了。我需要为 fxml 文件创建一个控制器类,并将 fx:controller="view.MainView" 替换为 fx:controller="view.MainViewController" 或任何我命名的控制器。谢谢!!知道您是如何通过该跟踪异常列表发现我的 MainView fxml 中有问题的,但谢谢
  • 哦,是的,您需要一个类来充当窗口的控制器。查找异常的原因并不难,只需查找“Caused by”行,它们会详细说明异常消息。其中有几个,因此请一个接一个地找到它们,直到您看到可用的消息。请注意,您可以编辑原始问题,因此您不应在评论中发布您的代码。
猜你喜欢
  • 2016-03-13
  • 2012-05-08
  • 2014-09-09
  • 2013-11-07
  • 2016-11-09
  • 1970-01-01
  • 2016-02-29
  • 2015-09-09
  • 2015-03-16
相关资源
最近更新 更多