当前位置: 首页 > 知识库问答 >
问题:

如何在FX:Include中指定资源?

李泓
2023-03-14
resources = ResourceBundle.getBundle(value, Locale.getDefault(),
FXMLLoader.this.resources.getClass().getClassLoader());

通过调试,我发现fxmlloader.this.resources为null,这导致了NPE。起初我以为我只是给出了一个错误的文件路径,但请注意,这发生在ResourceBundle甚至尝试加载属性文件之前!

有什么办法可以解决这个问题,这样资源就可以加载了吗?

下面是我的档案。它们都在同一个目录中(为了简单起见,默认包)。

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class TestApp extends Application
{
    @Override
    public void start(Stage primaryStage) throws Exception
    {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getClassLoader().getResource("Test1.fxml"));
        loader.load();

        Scene scene = new Scene((Parent) loader.getRoot());
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args)
    {
        launch(args);
    }
}
<?xml version="1.0" encoding="UTF-8"?>

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

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1">
   <center>
      <fx:include source="Test2.fxml" resources="test2" />
   </center>
</BorderPane>
<?xml version="1.0" encoding="UTF-8"?>

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

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1">
   <center>
      <Label text="%test" BorderPane.alignment="CENTER" />
   </center>
</BorderPane>
test=Test 2

完整StackTrace:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(LauncherImpl.java:182)
    at com.sun.javafx.application.LauncherImpl$$Lambda$50/1645995473.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException: 
/C:/Eclipse/fx/bin/Test1.fxml:9

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2605)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2583)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2445)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2413)
    at TestApp.start(TestApp.java:16)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863)
    at com.sun.javafx.application.LauncherImpl$$Lambda$53/1370226249.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl$$Lambda$45/186276003.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
    at com.sun.javafx.application.PlatformImpl$$Lambda$48/1026297962.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
    at com.sun.javafx.application.PlatformImpl$$Lambda$47/237061348.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
    at com.sun.glass.ui.win.WinApplication$$Lambda$36/2117255219.run(Unknown Source)
    ... 1 more
Caused by: java.lang.NullPointerException
    at javafx.fxml.FXMLLoader$IncludeElement.processAttribute(FXMLLoader.java:1088)
    at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:216)
    at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:740)
    at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2711)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2531)
    ... 17 more
Exception running application TestApp

共有1个答案

秦宏盛
2023-03-14

FXMLLoader中存在错误。要解决此错误,请执行以下操作:

  1. 加载根fxml文件时,必须为FXMLLoader实例定义根ResourceBundle。
  2. 根resourceBundle必须从另一个类加载器加载,然后才是引导类加载器。

可以在oracle论坛中找到一个可能的解决方法:https://community.oracle.com/message/11240449

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class TestApp extends Application
{
    @Override
    public void start(Stage primaryStage) throws Exception
    {
        FXMLLoader loader = new FXMLLoader();

        // add the resource wrapper
        loader.setResources(new ResourceWrapper());

        loader.setLocation(getClass().getClassLoader().getResource("Test1.fxml"));
        loader.load();

        Scene scene = new Scene((Parent) loader.getRoot());
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

    // an empty resource bundle
    private static class ResourceWrapper extends ListResourceBundle {
        @Override
        protected Object[][] getContents() {
            return new Object[0][];
        }
    }
}
 类似资料:
  • 我在redis stream文档中看到了这一点,它说: 是时候试着用消费者群体来阅读一些东西了: XREADGROUP回复与XREAD回复一样。请注意,尽管上面提供了一个组,但它声明我希望使用消费者组mygroup从流中读取数据,我是消费者Alice。每次使用者对使用者组执行操作时,都必须指定其名称,以便在组内唯一标识此使用者。 在上面的命令行中还有另一个非常重要的细节,在强制流选项之后,为密钥m

  • 我有一组文件想放在。由< code>mvn编译生成的jar。不幸的是,我希望将它们放在. jar内的特定路径中。com/purple frog/expg lsl/castle/main . glsl格式的jar文件 如何在pom中指定此映射。xml?我不能在不影响其他程序员工作流程的情况下弄乱源项目的目录层次结构。

  • 问题内容: 在网络上快速搜索发现三个或四个变种人如何被指定和在。 指定JPA 2.1版的“正确”方式是什么? 我正在使用 问题答案: 根据官方文档,它必须是(像您的一样):

  • 问题内容: 我想在3个地方使用相同的HTML模板,只是每次使用不同的模型。我知道我可以从模板访问变量,但是名称会有所不同。 有没有办法将模型传递给ngInclude? 这是我想要实现的,当然add-variable属性现在不起作用。然后,在我包含的模板中,我将访问detailsObject及其属性。 如果我使用ng-include的方法不好,还有其他应该尝试的方法吗? 问题答案: 注意:这不是我的

  • 问题内容: 我想像在常规j​​Query中那样指定。Angular.js有可能吗? 问题答案: 来自http://docs.angularjs.org/api/ng.$http 转换请求和响应可以使用转换函数来转换请求和响应。默认情况下,Angular应用以下转换: 请求转换: 如果请求配置对象的data属性包含一个对象,则将其序列化为JSON格式。响应转换: 如果检测到XSRF前缀,则将其剥离(

  • Spring Boot中有三个启动器:JarLauncher/Property tiesLauncher/WarLauncher。对于可执行jar,默认将使用JarLauncher。现在我想改用Property tiesLauncher,以便我可以使用外部类路径。我如何指定那是Spring boogradle插件? 根据D3。在这个文档D.3.1启动器清单中,我可以在清单中指定主类。MF如下: 但