workoutsmain.fxml:
<AnchorPane id="AnchorPane" fx:id="anchorPane" prefHeight="900.0" prefWidth="1600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="workouts.WorkoutsMainController">
<children>
<SplitPane fx:id="splitPane" dividerPositions="0.15" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<items>
<AnchorPane>
<children>
<StackPane fx:id="menuPane" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"/>
</children>
</AnchorPane>
<AnchorPane>
<children>
<Pane fx:id="statisticsPane" visible="false" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="130.0"/>
<Pane fx:id="workoutsPane" visible="false" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="130.0">
//... other FX elements
</Pane>
<Pane fx:id="calendarPane" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="130.0"/>
<StackPane fx:id="logoPane" onMouseClicked="#setLogo" AnchorPane.bottomAnchor="778.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"/>
</children>
</AnchorPane>
</items>
</SplitPane>
</children>
</AnchorPane>
WorkoutsMainController.java:
public class WorkoutsMainController implements Initializable, SplitPaneDividerController {
private final DataBase dataBase = new DataBase();
private boolean first = true;
private String logoName;
@FXML
private StackPane logoPane, menuPane;
private final List<Pane> panes = new ArrayList<>();
@FXML
private Pane calendarPane, workoutsPane, statisticsPane;
@FXML
private SplitPane splitPane;
@FXML
private AnchorPane anchorPane;
// methods and functions
@Override
public void initialize(URL url, ResourceBundle rb) {
setLogo();
disableSplitPaneDivider(splitPane, 0.1525);
setUpMainMenu();
}
}
java:
public class MainClass extends Application {
private Parent anchorPane;
private final String fxml = "WorkoutsMain.fxml";
@Override
public void start(Stage stage) {
try {
anchorPane = FXMLLoader.load(getClass().getResource(fxml));
} catch (IOException ex) {
System.out.println("Error when trying to load " + fxml);
}
Scene scene = new Scene(anchorPane);
stage.setScene(scene);
stage.getIcons().add(new Image(getClass().getResourceAsStream("/calendarIconWhite.png")));
stage.setTitle("THE WORKOUT CALENDAR 1.7.0 by hazazs®");
stage.setResizable(false);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
<Pane fx:id="workoutsPane" visible="false" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="130.0">
//... other FX elements
</Pane>
<fx:include source="Workouts.fxml" fx:id="workoutsPane"/>
<Pane fx:id="workoutsPane" visible="false" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="130.0" xmlns:fx="http://javafx.com/fxml/1" fx:controller="workouts.WorkoutsController">
// other FX elements
</Pane>
public class WorkoutsMainController implements Initializable, SplitPaneDividerController {
protected static final DataBase dataBase = new DataBase();
private final WorkoutsController workoutsController = new WorkoutsController();
private boolean first = true;
private String logoName;
@FXML
private StackPane logoPane, menuPane;
private final List<Pane> panes = new ArrayList<>();
@FXML
private Pane calendarPane, workoutsPane, statisticsPane;
@FXML
protected static SplitPane splitPane;
@FXML
protected static AnchorPane anchorPane;
// and so on..
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:498)
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:498)
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$159(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException: Root cannot be null
at javafx.scene.Scene.<init>(Scene.java:336)
at javafx.scene.Scene.<init>(Scene.java:194)
at workouts.MainClass.start(MainClass.java:23)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$166(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$179(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$177(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$178(PlatformImpl.java:294)
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$152(WinApplication.java:177)
... 1 more
有什么建议吗,伙计们,我应该做什么让这再次工作?有了一个。fxml和一个控制器,它工作得很好。
谢谢
您正在WorkoutsMainController
中创建一个新的WorkoutsController
实例,而不是使用FXMLLoader
为您创建的实例。您创建的实例中的@fxml
-notnated字段将全部为null,因此您可能会在某个地方得到一个null指针异常。(您应该在控制台中看到“尝试加载workoutsmain.fxml时出错”消息。)因此,start()
方法中的anchorpane
从未初始化,并且当将空的anchorpane
引用传递给scene
构造函数时,会出现异常。
注意:您已经(出于某种未知的原因)将splitpane
和anchorpane
:这样做没有意义,
FXMLLoader
不会初始化静态字段。所以这些字段在控制器中也是null。
如果不压缩通过加载FXML引发的异常,您将能够看到底层异常的实际堆栈跟踪。
修复措施有:
>
不要使@fxml
-带注释的字段静态化
。参见JavaFX8兼容性问题--FXML静态字段
要为包含的由FXMLLoader
创建的FXML注入实际的控制器实例,请参见文档中的嵌套控制器。简而言之,由于fx:include
具有fx:id=“workoutspane”
,因此应该替换
private final WorkoutsController workoutsController = new WorkoutsController();
@FXML private WorkoutsController workoutsPaneController ;
我有一个名为“AdjustmentBar”的类,它包括一个imageView、一个菜单栏和一些图片编辑函数(一个FXML文件和一个控制器)。AdjustmentBar被加载到另一个名为“ExaminationDisplayer”的类(也是一个FXML文件和一个控制器)。ExaminationDisplayer从一个在线MySQL数据库加载一些图像,我希望这些图像都显示在一个独立的ImageView
问题内容: 我试图从一个简单的ArrayList的内容填充ListView。这是我的 Controller.java文件: 这是我的Style.fxml文件: 该程序运行,但是我输入的歌曲没有出现在ListView中(并且Song类确实具有toString方法)。 我能做什么? 问题答案: 问题是没有将控制器的初始化为预期问题的质询者。 Java 8中FXMLLoader的操作有点奇怪。 如果您在
我一直试图弄清楚如何在另一个FXML文件中处理一个FXML文件。但我有个例外。:(这是我的内部FXML(视图包中的newinside.FXML): 这是它的控制器(controller包中的newController.java): 最后,这是应用程序包中的main.java: 当我单击该按钮时,它给了我很长的异常:“线程中的异常”JavaFX应用线程“java.lang.RuntimeExcept
我一直在做一个桌面应用程序,在那里我需要使用JavaFX。我在使用Scene Builder构建的eclipse上的JavaFX项目中创建了各种FXML文件。除了这个讨厌的文件(fxmlimglist.FXML)外,我加载任何FXML文件都没有问题。 其主要思想是,当按下一个按钮时,会出现一个新窗口。下面是该按钮的事件处理程序的代码: package explorer中的项目如下所示: https
我在相应的控制器中有一个用FXML创建的窗口,我有一个按钮,当单击特定的按钮时,它会加载一个小框。这个盒子也是使用FXML设计的。 当我加载盒子并想把它添加到窗口中时,我得到这个错误: 这是我的代码: 主窗口FXML: 我要动态创建的框的FXML: