为什么加载我的fxml时会出现stackoverflow? [英] Why am I getting a stackoverflow when loading my fxml?

查看:112
本文介绍了为什么加载我的fxml时会出现stackoverflow?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我调整了我的控制器构造函数和fxml,以便fxml到控制器的所有设置都在fxml中,除了FXML构造和fxml加载。这是我的控制器:

I've adjusted my controller constructor and fxml so that all setup of the fxml to the controller is in the fxml except for the FXML construction and the fxml loading. Here is my controller:

public class MainOverviewTab extends Tab {

@FXML private AnchorPane content;

public MainOverviewTab() {
    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("main_overview_tab.fxml"));
    // fxmlLoader.setRoot(content);
    // fxmlLoader.setController(this);      

    try {
        fxmlLoader.load();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

和我的fxml文件:

<AnchorPane id="AnchorPane" 
    fx:id="content" 
    fx:controller="dominion.application.controller.MainOverviewTab"
    ...other settings >

        <children>
            ....
        </children>
</AnchorPane>

调用fxmlLoader.load()并返回FXMLLoader fxmlLoader = new FXMLLoader时发生堆栈溢出(...)然后再次调用fxmlLoader.load()...为什么会发生这种情况?如何保持控制器构造函数相同并加载fxml相同?或者这是不可能的?

The stackoverflow occurs when the fxmlLoader.load() is called and goes back to FXMLLoader fxmlLoader = new FXMLLoader(...) and then fxmlLoader.load() is called again... Why is this happening and how do I keep my controller constructor the same and load the fxml the same? Or is this not possible?

推荐答案

你不应该在contructor中调用FXml loader。因为当你使用FXml加载器加载fxml文件时,它会一次又一次地递归创建MainOverviewTab。所以它会导致堆栈溢出错误。如果从构造函数中删除代码并从显式方法调用它将起作用。

You should not call FXml loader with in contructor. because when you load fxml file by using FXml loader , it will create MainOverviewTab again and again recursively. so it cause stack overflow error. If you remove the code from constructor and call from explicit method it will work.

public static void mainTabLoader() {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("main_overview_tab.fxml"));
    // fxmlLoader.setRoot(content);
    // fxmlLoader.setController(this);      

    try {
        fxmlLoader.load();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

这篇关于为什么加载我的fxml时会出现stackoverflow?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆