简体   繁体   English

为什么在加载 fxml 时会出现 stackoverflow?

[英]Why am I getting a stackoverflow when loading my 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.我已经调整了我的控制器构造函数和 fxml,以便控制器的 fxml 的所有设置都在 fxml 中,除了 FXML 构造和 fxml 加载。 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();
    }
}

and my fxml file:和我的 fxml 文件:

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

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

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?当调用 fxmlLoader.load() 并返回到 FXMLLoader fxmlLoader = new FXMLLoader(...) 然后再次调用 fxmlLoader.load() 时会发生堆栈溢出... 为什么会发生这种情况以及如何保持我的控制器构造函数相同并且加载 fxml 相同? Or is this not possible?或者这是不可能的?

You should not call FXml loader with in constructor.你不应该在构造函数中调用 FXml 加载器。 because when you load fxml file by using FXml loader , it will create MainOverviewTab again and again recursively.因为当您使用 FXml loader 加载 fxml 文件时,它会一次又一次地递归创建 MainOverviewTab。 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();
    }

} }

If you call setController(...) on your FXMLLoader instance, you should not specify the controller in the FXML file.如果您在 FXMLLoader 实例上调用 setController(...),则不应在 FXML 文件中指定控制器。 Remove the fx:controller attribute from the FXML file and uncomment the setRoot(...) and setController(...) calls and it should work.从 FXML 文件中删除 fx:controller 属性并取消注释 setRoot(...) 和 setController(...) 调用,它应该可以工作。

This is what happens when a controller constructor tries to load FXML with a fx:controller attribute naming it, it get infinitely recursive.当控制器构造函数尝试加载带有 fx:controller 属性命名的 FXML 时会发生这种情况,它会无限递归。 Been there... Clever use of fxmlLoader.setControllerFactory(factoryObject) can get around that.去过那里...巧妙地使用 fxmlLoader.setControllerFactory(factoryObject) 可以解决这个问题。

See my answer for an FXML based control retaining fx:controller attribute in root element, this can be adapted for other controller creation.请参阅我在根元素中保留 fx:controller 属性的基于 FXML 的控件的答案,这可以适用于其他控制器创建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在加载我的 FXML 文件时收到 javafx.fxml.LoadException? - Why am I receiving a javafx.fxml.LoadException when loading my FXML file? 为什么我会出现stackoverflow? - Why am I getting stackoverflow? 为什么我的数组索引0出现stackoverflow错误 - Why am I getting a stackoverflow error on index 0 in my array 为什么我得到NoClassDefFoundError异常而不是StackOverflow错误? - Why am I getting a NoClassDefFoundError exception rather than a StackOverflow error? 为什么在运行程序时出现NullPointerException? - Why am I getting a NullPointerException when I run my program? 为什么在运行应用程序时出现此错误? - Why am I getting this error when I run my app? 为什么在启动服务时出现“找不到激活器”的提示? - Why am I getting “Activator not found” when starting my service? 退出我的活动时为什么会崩溃? - Why am I getting a crash when exiting my Activity? 为什么在尝试与我的服务器通信时出现此错误? - Why am i getting this error when trying to communicate with my server? 为什么在并行化我的树搜索时会得到这个 output? - Why am I getting this output when parallelising my tree search?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM