Java FXML加载异常
我遇到了一个我自己绝对无法解决的问题,因为我刚刚开始使用JavaFX。 我得到了一个令人讨厌的javafx.fxml.LoadException:,但我做得和指南完全一样,但我无法运行我的主程序。 这是异常输出的最大值:Java FXML加载异常,java,exception,javafx-2,javafx-8,fxml,Java,Exception,Javafx 2,Javafx 8,Fxml,我遇到了一个我自己绝对无法解决的问题,因为我刚刚开始使用JavaFX。 我得到了一个令人讨厌的javafx.fxml.LoadException:,但我做得和指南完全一样,但我无法运行我的主程序。 这是异常输出的最大值: apr 07, 2014 4:06:37 EM application.Main start ALLVARLIG: null javafx.fxml.LoadException: /C:/Users/Jakob/Dropbox/java_kurser/Project%20Ti
apr 07, 2014 4:06:37 EM application.Main start
ALLVARLIG: null
javafx.fxml.LoadException:
/C:/Users/Jakob/Dropbox/java_kurser/Project%20Timeline/bin/application/LoginGUI.fxml
at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at application.Main.start(Main.java:21)
at com.sun.javafx.application.LauncherImpl$8.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl$7.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl$6$1.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl$6$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$6.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$300(Unknown Source)
at com.sun.glass.ui.win.WinApplication$4$1.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[2,6]
Message: Bearbetningsinstruktionens målmatchning "[xX][mM][lL]" är inte tillåten.
at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(Unknown Source)
at javax.xml.stream.util.StreamReaderDelegate.next(Unknown Source)
... 20 more
LoginController.java
package application;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
public class LoginController implements Initializable {
@FXML // ResourceBundle that was given to the FXMLLoader
private ResourceBundle resources;
@FXML // URL location of the FXML file that was given to the FXMLLoader
private URL location;
@FXML // fx:id="loginButton"
private Button loginButton; // Value injected by FXMLLoader
@FXML // fx:id="newUserButton"
private Button newUserButton; // Value injected by FXMLLoader
@FXML // fx:id="passwordField"
private PasswordField passwordField; // Value injected by FXMLLoader
@FXML // fx:id="usernameField"
private TextField usernameField; // Value injected by FXMLLoader
public void initialize(URL location, ResourceBundle resources) {
assert loginButton != null : "fx:id=\"loginButton\" was not injected: check your FXML file 'LoginGUI.fxml'.";
assert newUserButton != null : "fx:id=\"newUserButton\" was not injected: check your FXML file 'LoginGUI.fxml'.";
assert passwordField != null : "fx:id=\"passwordField\" was not injected: check your FXML file 'LoginGUI.fxml'.";
assert usernameField != null : "fx:id=\"usernameField\" was not injected: check your FXML file 'LoginGUI.fxml'.";
//The button event for the login button
loginButton.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
System.out.println("This button works");
}
});
}
}
问题出在源头 所以,你必须把它换成一个合适的 因此,请在此处编辑fxml文件的源代码
AnchorPane root = (AnchorPane) FXMLLoader.load(Main.class.getResource("LoginGUI.fxml"));
用这个
AnchorPane root = (AnchorPane) FXMLLoader.load(Main.class.getResource("/packagename/LoginGUI.fxml"));
我也经历过这个问题。我发现我没有将包名称添加到FXML文件中给定的控制器名称中;我最初只是添加了控制器类名。
例如:如果我的控制器类位于
com.stackoverflow.gui
包下,并且我的控制器类的名称是LoginController
。FXML文件应该有com.stackoverflow.gui.LoginController
,而不仅仅是LoginController
,我今天也遇到了同样的问题,对于那些可能有这个问题的人(像我一样)正在寻找答案:
您可能更改了控制器文件上的任何组件Id,并且忘记更新sceneBuilder(或FXML文件)上的Id(反之亦然),因此控制器无法链接视图文件上的组件
例如:
(在控制器文件上)
(在FXML文件或SceneBuilder上)
:-)请翻译:Bearbettingsinstrucktionens målmatching“[xX][mM][lL]”直到十点。处理指令目标匹配。。。。。。。。这是不允许的。在一开始:ALLVARLIG:null=severy:null不确定这是问题所在,但main方法是不寻常的。您只需执行
launch(args)代码>。但是,无论您做什么,为args数组传递null引用都是一个非常糟糕的主意<代码>Application.launch(Main.class,新字符串[0])如果您(出于某种原因)希望强制它忽略命令行参数,则“代码>”会安全得多。@James\u\D哦,您真是个天使;)谢谢我按照您的建议使用新字符串[0]编写,现在它已启动!:D:D再次感谢:D现在我知道我肯定也希望不会再犯同样的错误了。啊,该死的Java fx。我现在讨厌它:(现在它的问题和以前一样,但我的问题和昨天它工作时的问题一模一样。我不知道我会怎么做。也许扔掉我的电脑吧
AnchorPane root = (AnchorPane) FXMLLoader.load(Main.class.getResource("LoginGUI.fxml"));
AnchorPane root = (AnchorPane) FXMLLoader.load(Main.class.getResource("/packagename/LoginGUI.fxml"));
@FXML
Button btnName1
fx:id="btnName_1"