Afterburner.fx 对我来说已经坏了,我似乎无法让它再次工作。
在我的主要方法中,我有这个:
@Override
public void start(Stage primaryStage) throws Exception{
Map<Object, Object> customProperties = new HashMap<>();
Injector.setConfigurationSource(customProperties::get);
final StackPane views = new StackPane();
customProperties.put("views", views);
BattleView battleView = new BattleView();
Parent view = battleView.getView();
view.setVisible( false );
view.setId("battle");
views.getChildren().add(view);
...
}
然而,当我到达 BattleView BattleView = new BattleView();
行时,我似乎遇到了异常。 Afterburner fx 似乎试图在 ui.battle.BattleView
上评估 toString()
,但它不喜欢它,请参见下图:
在终端结果中:
我没有从类似问题中找到任何帮助,所以希望有人能指出我正确的方向!救命!
编辑:将Battle.css和battle.fxml移动到resources/ui/battle后出现同样的错误:
最佳答案
由于您使用的是 Java 11 和模块系统,因此您的资源输出目录必须与其所属模块位于同一输出目录中。
从您帖子中的图片来看,您正在使用 gradle 并直接从 IDEA 构建项目。
您需要告诉 IDE 将资源复制到同一输出目录:
//build.gradle
plugins {
...
id 'idea' //add idea plugin
}
...
//put compiled classes and resources in the same directory (change the path if needed)
idea.module.outputDir file("out/production/classes")
如果您也打算使用 gradle 进行构建:
//put compiled classes and resources in the same directory (change the paths if needed)
sourceSets.main.output.resourcesDir = "build/classes/java/main"
sourceSets.test.output.resourcesDir = "build/classes/java/test"
您可能需要打开包含资源文件的包,以便框架可以访问它们:
//module-info.java
module YourModuleName {
...
opens ui.battle to afterburner.fx; //add this
}
编辑:
The 'idea' in 'idea.module.outputDir' is greyed out with the error 'Cannot resolve symbol 'idea''.
这并不是一个真正的错误,但您可以轻松地消除它:
//use this format instead of the previous one
idea {
module.outputDir file("out/production/classes")
}
Im getting an nullPointer error from input streams where I am trying to load resources, such as: setImage(new Image(getClass().getClassLoader().getResourceAsStream("images/ui/mineStartButton.png")));
不要调用 getClassLoader()
,否则您需要打开包含图像资源的包 - 即使调用代码位于同一模块中(检查 Javadoc ):
//you need to add '/' at the beginning of the path string to make it absolute
getClass().getResourceAsStream("/images/ui/mineStartButton.png")
关于java - 尝试创建 View 时使用 afterburner.fx 的 NullPointer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52493819/