加载FXML时发生javafx.fxml.LoadException

时间:2015-06-20 22:36:11

标签: java intellij-idea fxml fxmlloader

我正在尝试使用黑色背景创建一个非常简单的VBox。就这样。没有其他的。我使用FXML来描述我的VBox

sample.fxml:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.VBox?>

<VBox xmlns:fx="http://javafx.com/fxml" fx:controller="sample.Controller" alignment="CENTER" background="#000000ff">
</VBox>   

Controller.java:

package sample;

public class Controller {  
    // No code written. Not required.
}  

Main.java:

package sample;

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


/* Code auto generated by IDEA IDE. */
public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Adi's login app");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    } 


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

我正在使用IDEA IntelliJ IDE。我收到以下错误:

Exception in Application start method
Exception in thread "main" 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$2/51228289.run(Unknown Source)
	at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException: 
/Users/aditya/Desktop/workspace/Java/LoginApp/out/production/LoginApp/sample/sample.fxml:5

	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.loadImpl(FXMLLoader.java:3218)
	at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3179)
	at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3152)
	at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3128)
	at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3108)
	at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3101)
	at sample.Main.start(Main.java:13)
	at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863)
	at com.sun.javafx.application.LauncherImpl$$Lambda$53/87719178.run(Unknown Source)
	at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
	at com.sun.javafx.application.PlatformImpl$$Lambda$49/2001689116.run(Unknown Source)
	at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
	at com.sun.javafx.application.PlatformImpl$$Lambda$51/779320874.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$50/1474675095.run(Unknown Source)
	at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Caused by: java.lang.IllegalArgumentException: Unable to coerce #000000ff to class javafx.scene.layout.Background.
	at com.sun.javafx.fxml.BeanAdapter.coerce(BeanAdapter.java:495)
	at com.sun.javafx.fxml.BeanAdapter.put(BeanAdapter.java:258)
	at com.sun.javafx.fxml.BeanAdapter.put(BeanAdapter.java:54)
	at javafx.fxml.FXMLLoader$Element.applyProperty(FXMLLoader.java:508)
	at javafx.fxml.FXMLLoader$Element.processValue(FXMLLoader.java:359)
	at javafx.fxml.FXMLLoader$Element.processPropertyAttribute(FXMLLoader.java:321)
	at javafx.fxml.FXMLLoader$Element.processInstancePropertyAttributes(FXMLLoader.java:231)
	at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:763)
	at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2827)
	at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2536)
	... 18 more

为什么会这样?如果我犯了任何错误或者我错过了什么,请告诉我。

1 个答案:

答案 0 :(得分:0)

与Intellij无关,我也有这个问题。  我正在使用Java 1.8编译,我改为1.7并且工作。