java - 未设置位置
问题描述
我在尝试运行我的代码时遇到了这个错误,这是一个简单的 javafx 代码来加载一个 fxml 文件,我尝试了我在这里找到的解决方案,但没有什么对我有用。对不起,如果格式不是很好,这是我在这里的第一篇文章,对不起,如果我正在屠杀语言,英语不是我的第一语言。提前致谢!
package projeto;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import java.io.IOException;
public class MainApp extends Application {
private Stage primaryStage;
private static BorderPane rootLayout;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("CineTudo");
initRootLayout();
showFilmeOverview();
}
public void initRootLayout(){
try {
//Carrega o layout root do arquivo fxml
final FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("/resources/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();
Scene cena = new Scene(rootLayout);
primaryStage.setScene(cena);
primaryStage.show();
} catch(IOException e) {
e.printStackTrace();
}
}
public static void showFilmeOverview() {
try {
final FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("/resources/FilmeOverview.fxml"));
AnchorPane filmeOverview = (AnchorPane) loader.load();
rootLayout.setCenter(filmeOverview);
}catch (IOException e){
e.printStackTrace();
}
}
public Stage getPrimaryStage() {
return primaryStage;
}
}
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$154(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalStateException: Location is not set.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2434)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at projeto.MainApp.initRootLayout(MainApp.java:34)
at projeto.MainApp.start(MainApp.java:25)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(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$147(WinApplication.java:177)
... 1 more
Exception running application projeto.MainApp
解决方案
FXMLLoader
Location is not set
当输入为 时抛出“ ” null
。
在您的情况下,这是因为您提供了错误的 XML 绝对路径。XML 的绝对路径应该是/projeto/resources/RootLayout.fxml
:
要么修复绝对路径:
new FXMLLoader(MainApp.class.getResource("/projeto/resources/RootLayout.fxml"));
或者使用相对路径:
new FXMLLoader(MainApp.class.getResource("resources/RootLayout.fxml"));
(注意缺少领先/
)
推荐阅读
- linux - 将参数从 alsa 应用程序传递到内核驱动程序
- javascript - 使用 jQuery Chosen 时隐藏选择列表选项值
- haskell - 仆人中的安全链接
- azure-devops - 自托管代理是否需要专用机器?
- php - 准备好的语句返回错误号 0
- iot - 如何使用 DO + OpenVPN 访问 LAN IOT 设备?
- python - 是否可以使用整体 rate_func 运行多个动画?
- excel - 使用 range.exporttopdf 方法时将图表对象保留在一张纸上的 Vba 代码
- java - 更改操作栏图标位置
- angular - 角 Nginx 码头工人 404