【问题标题】:Exception in Application Start method caused by initialize method初始化方法导致的应用程序启动方法异常
【发布时间】:2019-04-18 06:11:28
【问题描述】:

这是我第一个使用 FXML 的项目之一,我在初始化方法和加载 FXML 文件时遇到了问题。

我不确定我缺少什么,但如果对程序有任何帮助,我们将不胜感激。

控制器类的摘录:

public class Controller implements Initializable {

    @FXML TextField input;
    @FXML TextArea output;
    @FXML RadioMenuItem chooseLonghand;
    @FXML RadioMenuItem chooseRoman;
    @FXML Button addInput;
    String number;
    Object conversion;
    Alert alert;
    BufferedNumberConverter converter;

    TextInputDialog retry = new TextInputDialog("Enter a valid integer");

    public Controller(){

    }

    @FXML
    public void initialize(URL url, ResourceBundle resourceBundle) {
        final ToggleGroup group = new ToggleGroup();
        chooseLonghand.setToggleGroup(group);
        chooseRoman.setToggleGroup(group);
    }

启动器类:

public class Launcher extends Application {
    public static void main(String [] args) {
        Application.launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("GUI.fxml"));
        stage.setScene(new Scene(root));
        stage.setTitle("Number Converter");
        stage.show();

        // Close all windows when primaryStage closed
        stage.setOnCloseRequest(ev -> System.exit(0));

    }
}

FXML:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.RadioButton?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.Pane?>

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ragog.Controller">
   <children>
      <TextArea layoutY="200.0" prefHeight="200.0" prefWidth="600.0" />
      <Label layoutX="14.0" layoutY="14.0" prefHeight="17.0" prefWidth="91.0" text="Enter a number:" />
      <TextField layoutX="105.0" layoutY="10.0" />
      <Button layoutX="265.0" layoutY="10.0" mnemonicParsing="false" onAction="#addInput" text="Add Input" />
      <RadioButton layoutX="160.0" layoutY="56.0" mnemonicParsing="false" onAction="#chooseLonghand" text="Rewrite in longhand" />
      <RadioButton layoutX="160.0" layoutY="85.0" mnemonicParsing="false" onAction="#chooseRoman" prefHeight="17.0" prefWidth="177.0" text="Rewrite in roman numerals" />
      <Button layoutX="355.0" layoutY="81.0" mnemonicParsing="false" onAction="#rewrite" text="Rewrite!" />
      <Button layoutX="14.0" layoutY="158.0" mnemonicParsing="false" onAction="#output" text="Output:" />
   </children>
</Pane>

【问题讨论】:

    标签: java javafx fxml


    【解决方案1】:

    chooseLonghandchooseRoman 在 fxml 文件中都没有对应的元素。对于 &lt;RadioButton&gt; 元素,您只能使用具有相同名称的 onAction 事件处理程序。 (我不确定为什么在 FXMLLoader 甚至到达调用 initialize 的地步之前这不会导致异常。)

    因此,当调用initialize 方法时,两个字段都是null

    您需要将fx:ids 添加到应注入控制器的元素中:

    <RadioButton fx:id="chooseLonghand" layoutX="160.0" layoutY="56.0" mnemonicParsing="false" onAction="#chooseLonghand" text="Rewrite in longhand" />
    <RadioButton fx:id="chooseRoman" layoutX="160.0" layoutY="85.0" mnemonicParsing="false" onAction="#chooseRoman" prefHeight="17.0" prefWidth="177.0" text="Rewrite in roman numerals" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-18
      • 2018-05-29
      相关资源
      最近更新 更多