无论如何,FXMLLoader.getController()都返回null

时间:2016-06-09 22:16:36

标签: java javafx

我一直在为JavaFX的学习做一个小应用程序。在应用程序中,我需要将控制器与主文件分开,所以我设计了这个:

public class DiceObserver implements Observer {

    FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml"));
    public Controller controller = loader.<Controller>getController();

    Dice ov;
    public DiceObserver(Dice dice){
        this.ov = dice;
    }

    public void update(Observable o, Object arg) {
        int roll = ov.getLastValue().getRolledValue();
        System.out.println(roll);
        controller.updateBarChart(roll);
    }
}

FXML文件:

package sample;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Button;
import javafx.scene.control.Slider;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;


public class Controller{
    @FXML public TextField freqText;
    @FXML public Slider freqSlider;
    @FXML public Button startButton;
    @FXML public Button endButton;
    @FXML public BarChart rollChart;
    @FXML public ImageView dicePic;
    XYChart.Series s1 = new XYChart.Series<>();
    XYChart.Series s2 = new XYChart.Series<>();
    XYChart.Series s3 = new XYChart.Series<>();
    XYChart.Series s4 = new XYChart.Series<>();
    XYChart.Series s5 = new XYChart.Series<>();
    XYChart.Series s6 = new XYChart.Series<>();
    Image side1 = new Image("Dice_Side_1.png");
    SimulationThread t = SimulationThread.getInstance();

    public void startSimulation(ActionEvent event) { // start the simulation
        t.start();
    }

    public void getNewFreqValue(ActionEvent event) {
        freqText.setText(String.valueOf(freqSlider.getValue()));
    }

    public void stopSimulation(ActionEvent event) {
        t.end();
    }

    public void updateBarChart(int diceRoll){
        System.out.println(diceRoll);
        int rollval = diceRoll;
        switch (rollval) {
            case 1:
                s1.getData().add(rollval);
                break;
            case 2:
                s2.getData().add(rollval);
                break;
            case 3:
                s3.getData().add(rollval);
                break;
            case 4:
                s4.getData().add(rollval);
                break;
            case 5:
                s5.getData().add(rollval);
                break;
            case 6:
                s6.getData().add(rollval);
                break;
        }
    }

    public void updateDicePic(int diceRoll){
        dicePic.setImage(side1);
    }

    public void initialize() {
    }
}

updateBarChart方法位于控制器中。当我运行代码时,加载器有一个值,但getcontroller方法返回null。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

它返回<android.support.design.widget.CoordinatorLayout> <android.support.design.widget.AppBarLayout android:background="#00000000"> <android.support.design.widget.CollapsingToolbarLayout app:layout_scrollFlags="scroll|exitUntilCollapsed"> <com.android.myapp.MyCoolView app:layout_collapseMode="parallax"/> </android.support.design.widget.CollapsingToolbarLayout> </android.support.design.widget.AppBarLayout> <android.support.v7.widget.RecyclerView/> </android.support.design.widget.CoordinatorLayout> ,因为您尚未指定控制器。从您上面发布的代码中,您的控制器和FXML彼此之间并不了解。

呼叫:

null

示例:

https://github.com/SnakeDoc/superD/blob/master/src/com/vanomaly/superd/Main.java#L39

或者您必须在FXML文档中指定控制器(在根元素中):

loader.setController(yourControllerInstance);

示例:

https://github.com/SnakeDoc/Narvaro/blob/master/src/resources/Narvaro.fxml#L13

答案 1 :(得分:0)

FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml"));
public Controller controller = loader.<Controller>getController();

既没有设置控制器,也没有初始化fxml文件(fx:controller属性)中指定的控制器类,因为在使用getController()之前没有调用load()

我猜你正试图做后者。但是,如果没有实际加载fxml文件,则不会创建任何控制器。仅创建FXMLLoader实例不会加载任何内容。调用load方法时会处理输入。 (处理输入当然是了解并因此能够创建控制器实例的先决条件。)