2014-11-24 47 views
2

我嘗試在我的項目中使用Afterburner.fx作爲DI。我採取followme.fx的例子,我試圖申請我的項目。但我不知道,因爲我按照例如但是當我運行的應用程序,我得到這些例外,什麼是錯的:Afterburner.fx fxml加載錯誤

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:497) 
    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:497) 
    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$151(LauncherImpl.java:182) 
    at com.sun.javafx.application.LauncherImpl$$Lambda$50/1030870354.run(Unknown Source) 
    at java.lang.Thread.run(Thread.java:745) 
Caused by: java.lang.IllegalStateException: Location is not set. 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2438) 
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2413) 
    at com.airhacks.afterburner.views.FXMLView.loadSynchronously(FXMLView.java:87) 
    at com.airhacks.afterburner.views.FXMLView.initializeFXMLLoader(FXMLView.java:96) 
    at com.airhacks.afterburner.views.FXMLView.getView(FXMLView.java:108) 
    at com.******.controladores.MainApp.start(MainApp.java:44) 
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$158(LauncherImpl.java:863) 
    at com.sun.javafx.application.LauncherImpl$$Lambda$53/1821793753.run(Unknown Source) 
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$171(PlatformImpl.java:326) 
    at com.sun.javafx.application.PlatformImpl$$Lambda$46/1637506559.run(Unknown Source) 
    at com.sun.javafx.application.PlatformImpl.lambda$null$169(PlatformImpl.java:295) 
    at com.sun.javafx.application.PlatformImpl$$Lambda$48/313834886.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$170(PlatformImpl.java:294) 
    at com.sun.javafx.application.PlatformImpl$$Lambda$47/2117255219.run(Unknown Source) 
    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$144(WinApplication.java:101) 
    at com.sun.glass.ui.win.WinApplication$$Lambda$36/764308918.run(Unknown Source) 
    ... 1 more 
Exception running application com.******.controladores.MainApp 

目錄結構:

Files that I have in my project

主類

package com.******.controladores; 

import com.airhacks.afterburner.injection.Injector; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 
import java.util.logging.Logger; 
import static java.util.logging.Logger.getLogger; 
import javafx.application.Application; 
import static javafx.application.Application.launch; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 

public class MainApp extends Application { 

    private static final Logger LOG = getLogger(MainApp.class.getName()); 

    @Override 
    public void start(Stage stage) {   
     Map<Object, Object> customProperties = new HashMap<>(); 
     Injector.setConfigurationSource(customProperties::get); 
     LoginView loginView = new LoginView(); 
     Scene scene = new Scene(loginView.getView()); 
     stage.setScene(scene); 
     stage.show(); 
    } 

    @Override 
    public void stop() throws Exception { 
     Injector.forgetAll(); 
    } 

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

} 

LoginView類

package com.******.controladores; 

import com.airhacks.afterburner.views.FXMLView; 

public class LoginView extends FXMLView { 

} 

LoginPresenter類

package com.****.controladores; 

import com.****.dao.usuarioDAO; 
import com.****.modelos.usuario; 
import java.net.URL; 
import java.util.List; 
import java.util.ResourceBundle; 
import java.util.logging.Logger; 
import static java.util.logging.Logger.getLogger; 
import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.fxml.Initializable; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.ChoiceBox; 
import javafx.scene.control.PasswordField; 
import javafx.scene.image.Image; 
import javafx.scene.input.KeyCode; 
import javafx.scene.input.KeyEvent; 
import javafx.stage.Stage; 
import javafx.stage.WindowEvent; 
import javax.inject.Inject; 

public class LoginPresenter implements Initializable { 

    private static final Logger LOG = getLogger(LoginPresenter.class.getName()); 

    @FXML 
    Button btnAceptar; 
    @FXML 
    ChoiceBox lstUsuario; 
    @FXML 
    PasswordField txtClave; 

    @Inject 
    private DataModel datos; 

    @Override 
    public void initialize(URL url, ResourceBundle rb) { 
     List<Usuario> usuarios = new UsuarioDAO().obtenListausuariosPorEstado(true); 

     lstUsuario.getItems().addAll(usuarios); 
     btnAceptar.setOnAction((ActionEvent event) -> { 
      validarCampos(); 
     }); 
     txtClave.setOnKeyPressed((KeyEvent ke) -> { 
      if (ke.getCode().equals(KeyCode.ENTER)) { 
       validarCampos(); 
      } 
     }); 
    } 
} 

Login.fxml

<?xml version="1.0" encoding="UTF-8"?> 

<?import java.lang.*?> 
<?import javafx.scene.control.*?> 
<?import javafx.scene.image.*?> 
<?import javafx.scene.layout.*?> 
<?import javafx.scene.text.*?> 


<VBox alignment="CENTER" prefHeight="338.0" prefWidth="286.0" styleClass="fondo" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.****.controladores.LoginPresenter"> 
    <children> 
     <Label alignment="CENTER" contentDisplay="CENTER" nodeOrientation="LEFT_TO_RIGHT" styleClass="titulo" text="Inicio de sesión"> 
      <font> 
       <Font name="Segoe UI" size="30.0" /> 
      </font> 
     </Label> 
     <Pane prefHeight="59.0" prefWidth="286.0" /> 
     <VBox alignment="CENTER"> 
      <children> 
       <Label text="Usuario"> 
        <font> 
         <Font name="System Bold" size="12.0" /> 
        </font> 
       </Label> 
       <ChoiceBox fx:id="lstUsuario" prefWidth="150.0" /> 
       <Label text="Clave"> 
        <font> 
         <Font name="System Bold" size="12.0" /> 
        </font> 
       </Label> 
       <PasswordField fx:id="txtClave" promptText="Introduce tu clave" /> 
      </children> 
     </VBox> 
     <Pane prefHeight="59.0" prefWidth="286.0" /> 
     <Button fx:id="btnAceptar" contentDisplay="CENTER" mnemonicParsing="false" text="Aceptar" textAlignment="CENTER"> 
     </Button> 
     <Pane prefHeight="59.0" prefWidth="286.0" /> 
    </children> 
</VBox> 

POM

<dependency> 
     <groupId>com.airhacks</groupId> 
     <artifactId>afterburner.fx</artifactId> 
     <version>1.6.0</version> 
    </dependency> 
+0

我覺得FXML文件名應該是'Login.fxml'(不'login.fxml') – 2014-11-24 18:13:58

+0

我嘗試,但我得到同樣的錯誤。 – Marcos 2014-11-24 18:22:08

+0

@Marcos你的代碼正在爲我工​​作。所以我只是用其他名稱(Login2.fxml)重命名了'Login.fmxl',並得到了同樣的異常'Location is not set'。如果我把文件放在其他包中,也會發生同樣的情況。所以請檢查你是否在同一個軟件包中有這個文件。你也可以通過在'start()'手動加載它來檢查它:'FXMLLoader loader = new FXMLLoader(getClass()。getResource(「Login.fxml」));嘗試{loader.load(); } catch(IOException e){}'(不要忘記稍後將其刪除) – 2014-11-24 20:23:41

回答

5
Caused by: java.lang.IllegalStateException: Location is not set. 

也許你沒有在JAR * .fxml文件。我有同樣的問題。試着資源篩選添加到您的POM文件,喜歡這裏:https://github.com/AdamBien/afterburner.fx/blob/master/pom.xml

<resources> 
     <resource> 
      <directory>src/main/java</directory> 
      <includes> 
       <include>**/*.fxml</include> 
       <include>**/*.css</include> 
       <include>**/*.properties</include> 
      </includes> 
     </resource> 
     <resource> 
      <directory>src/test/java</directory> 
      <includes> 
       <include>**/*.fxml</include> 
       <include>**/*.css</include> 
       <include>**/*.properties</include> 
      </includes> 
     </resource> 
     <resource> 
      <directory>src/main/resources</directory> 
      <includes> 
       <include>**/*.xml</include> 
       <include>**/*.css</include> 
       <include>**/*.css</include> 
       <include>**/*.properties</include> 
      </includes> 
     </resource> 
    </resources> 

這允許將文件複製到.fxml在包JAR文件。

3

這可能不會幫助原始的海報,因爲這是現在10個月大,但我也陷入了同樣的問題。

我.fxml在我的罐子內的適當位置。我發現的修復程序是fxml文件名必須全部小寫。

我的設置是DevicePanelPresenter.java,DevicePanelView.java和devicePanel.fxml。這始終會引發以下異常

Caused by: java.lang.IllegalStateException: Location is not set. 
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2438) 
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2413) 
at com.airhacks.afterburner.views.FXMLView.loadSynchronously(FXMLView.java:87) 
at com.airhacks.afterburner.views.FXMLView.initializeFXMLLoader(FXMLView.java:96) 
at com.airhacks.afterburner.views.FXMLView.getPresenter(FXMLView.java:175) 
at com.zai.rackit.views.main.MainPresenter.initialize(MainPresenter.java:47) 
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552) 

通過加力代碼跟蹤,我發現方法

String getConventionalName() { 
    String clazz = this.getClass().getSimpleName().toLowerCase(); 
    return stripEnding(clazz); 
} 

所以我改變了我的FXML到devicepanel.fxml和現在的作品。