java - 在 javaFX 8 中添加 Button 节点会引发异常 - javafx.fxml.LoadException : Invalid identifier

标签 java exception intellij-idea javafx-8 fxml

我在我创建的堆栈 Pane 布局中添加了两个 ImageView ,但是当我尝试在我的 StackPane 布局中添加按钮节点时,虽然图像工作正常,但按钮不允许程序运行并抛出这个我无法弄清楚的异常:

> > 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:748)
Caused by: javafx.fxml.LoadException: Invalid identifier.
/C:/Users/Steli/Dropbox/Java_Projects/IntelliJ%20IDEA%20Projects/DokkanCardsPreview/out/production/DokkanCardsPreview/fxml/home.fxml:33
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
at javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:901)
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.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 com.spdesigns.dokkancardspreview.Main.start(Main.java:13)
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
Exception running application com.spdesigns.dokkancardspreview.Main

Process finished with exit code 1

现在这是我的 fxml 文件:

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

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<?import javafx.scene.image.ImageView?>
<?import javafx.scene.image.Image?>
<GridPane xmlns="http://javafx.com/javafx"
          xmlns:fx="http://javafx.com/fxml"
          fx:controller="com.spdesigns.dokkancardspreview.controllers.home"
          stylesheets="/css/main.css"
          fx:id="container"
          prefHeight="400.0" prefWidth="600.0">

    <children>
        <Label text="Cards List" fx:id="listTitle" GridPane.rowIndex="0" GridPane.columnIndex="0"/>
        <HBox fx:id="listViewAndImageContainer">
                <children>
                    <ListView fx:id="listView" GridPane.rowIndex="1" GridPane.columnIndex="0" prefHeight="600"/>
                </children>
            <children>
                <StackPane fx:id="ImageAndArrowButtonContainer">
                    <children>
                        <ImageView fx:id="imageView" fitWidth="400" fitHeight="600">
                           <Image url="/images/image_placeholder.png" fx:id="image"/>
                        </ImageView>
                        <ImageView fx:id="arrow" fitHeight="40" fitWidth="60">
                            <Image url="/images/arrow.png"/>
                        </ImageView>
                        <Button fx:id="arrow-button" prefHeight="40" prefWidth="60"  text="test"></Button>
                    </children>
                </StackPane>
            </children>
        </HBox>
    </children>

</GridPane>

这是 main.java - 粘贴它,因为它在异常中被提及 - :

package com.spdesigns.dokkancardspreview;

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



public class Main extends Application {    @Override
    public void start(Stage primaryStage) throws Exception{
         Parent root = FXMLLoader.load(getClass().getResource("/fxml/home.fxml"));
        primaryStage.setTitle("Dokkan Battle Card Preview");
        primaryStage.setScene(new Scene(root, 900, 700));
        primaryStage.setResizable(false);
        primaryStage.show();
    }


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

希望这里有人可以帮助我解决这个问题(这可能是一些“愚蠢”的东西,但仍然......我似乎无法找到这个异常的解决方案!

最佳答案

您不能在 id 中使用破折号 (-),因为它表示减号操作。将其替换为下划线 (_) 或使用驼峰命名法。

关于java - 在 javaFX 8 中添加 Button 节点会引发异常 - javafx.fxml.LoadException : Invalid identifier,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46264139/

相关文章:

Scala 代码在 Intellij 上运行,但不在 Apache Zeppelin notebook 上运行

intellij-idea - 我可以在 Intellij 结构搜索中使用过滤器来引用变量计数吗?

java - 使用递归 Java 镜像三角形

java - 在 Java 中创建包含 RXO 段的正确 HL7 消息

java - 写入文件异常?

c++ - 未捕获的 std::exception 在核心中不正确的堆栈跟踪

java - 将jar库添加到Maven项目

java - 使用JsonReader读取动态json文件

java - 如何判断我的程序是作为 Applet 运行还是作为 Java 应用程序运行

java - 在 JavaFX WorkerStateEvent 上编译 lambda 时出错