使用scenebuilder / fxml添加到Listview时出现问题

问题描述

因此,即时通讯在尝试将项目添加到我使用SceneBuilder添加的列表视图时遇到了困难。我将控件设置为要尝试使用的类的fxml,但是我试图在主类中填充列表视图,但它始终给出null指针异常 下面是主要班级

import javafx.stage.Stage;

import java.io.IOException;

/**
 * JavaFX App
 */
public class App extends Application {
    @FXML Listview list;
    private static Scene scene;

    @Override
    public void start(Stage stage) throws IOException {
        SecondaryController control = new SecondaryController();
        control.fill();
        scene = new Scene(loadFXML("primary"),640,480);
        stage.setScene(scene);
        stage.show();
    }

    static void setRoot(String fxml) throws IOException {
        scene.setRoot(loadFXML(fxml));
    }

    private static Parent loadFXML(String fxml) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource(fxml + ".fxml"));
        return fxmlLoader.load();
    }

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

}

这是连接到具有列表视图的fxml文件的类

package org.example;

import java.io.IOException;
import javafx.fxml.FXML;
import javafx.scene.control.ListView;

public class SecondaryController {
    public SecondaryController()
    {

    }
    @FXML private ListView list;

    public void fill()
    {
        list.getItems().addAll("red","blue","orange");

    }
    @FXML
    private void switchToDelete() throws IOException {

        App.setRoot("Delete");
    }
    @FXML
    private void switchToAdd() throws IOException {

        App.setRoot("Add");
    }

}

这是flist文件,里面保存了列表视图

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

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

<AnchorPane 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="org.example.SecondaryController">
   <children>
      <ListView fx:id="list" layoutX="91.0" layoutY="79.0" prefHeight="200.0" prefWidth="348.0" />
      <Button layoutX="30.0" layoutY="352.0" mnemonicParsing="false" onAction="#switchToDelete" text="Delete" />
      <Button layoutX="139.0" layoutY="352.0" mnemonicParsing="false" onAction="#switchToAdd" text="Add" />
   </children>
</AnchorPane>

我想正确地填充它,以至于不明白为什么它总是给出空指针异常。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...