【问题标题】:Why can't I launch this simple FXML hello world application?为什么我不能启动这个简单的 FXML hello world 应用程序?
【发布时间】:2015-12-20 16:09:17
【问题描述】:

我目前正在将我匆忙制作的 JavaFX 应用程序布局从 Java 迁移到 FXML,因此我可以分别关注样式、视图、控制器和模型,但我似乎无法运行简单的 hello world 应用程序。

我收到以下错误:

 Exception in Application start method
 java.lang.reflect.InvocationTargetException

这里是 View.fxml 文件

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<VBox prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml" fx:controller="calend.ae.Controller">

    <Label text="I love bacon"/>
    <Button text="Submit"/>

</VBox>

和主类

package calend.ae;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Model extends Application {

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

    @Override
    public void start(Stage stg) throws Exception{
        Parent tabs = FXMLLoader.load(getClass().getResource("View.fxml"));
        stg.setTitle("Hello World");
        stg.setScene(new Scene(tabs, 450, 250));
        stg.show();
    }
}

您可以找到完整的源代码here。我只想知道为什么它不运行,这里的上下文是实际的application

这是完整的堆栈跟踪:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Location is required.
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.load(Unknown Source)
    at calend.ae.Model.start(Model.java:17)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(Unknown Source)
    ... 1 more
Exception running application calend.ae.Model
Picked up _JAVA_OPTIONS: -Xmx512M

【问题讨论】:

  • 在此处发布您的代码,而不是在外部站点中
  • 同时发布完整的堆栈跟踪。
  • @James_D 已发布,谢谢。
  • @Reimus 已发布,谢谢。
  • 有几个关于这个错误的问题:你看过其中的任何一个吗?例如。 stackoverflow.com/questions/28266492/…

标签: java javafx javafx-8 fxml


【解决方案1】:

使用 maven 还是 ant?

如果你使用 ant,fxml 应该放在 Model.java 所在的同一个目录中

【讨论】:

  • 我不知道是哪个,我在(大部分)默认设置上使用eclipse,但是我在根目录和Model.java旁边都试过了,没有效果。
  • 如果它是 maven,你的 src 目录中可能有 2 个文件夹。 "java" 和 "resources" 在资源中创建一个 fxml 文件夹并将 fxml 文件放在那里。
  • 啊,现在可以了,github桌面程序将fxml文件放回model.java旁边,我猜它在同步之前从那里删除了它。谢谢
猜你喜欢
  • 2017-09-07
  • 2015-05-13
  • 2021-01-20
  • 2014-08-05
  • 1970-01-01
  • 2015-02-11
  • 1970-01-01
相关资源
最近更新 更多