Application start方法中的异常 [英] Exception in Application start method

查看:148
本文介绍了Application start方法中的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是javafx的新手,我正试图制作一个gui,当点击一个按钮时,它会转到另一个窗口。我尝试阅读Stack Overflow上的许多答案。我甚至尝试从一开始就重新制作项目...但我继续得到同样的错误。

I'm new to javafx and i was trying to make a gui from which when a button is clicked, it would go to another window. I tried reading many answers found on Stack Overflow. I even tried making the project again from the beginning...But i keep on getting the same error.

这就是异常是应用程序启动方法
java。 lang.reflect.InvocationTargetException javafx

That is Exception is Application start method java.lang.reflect.InvocationTargetException javafx

My Main

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

public class SkyTravelsFx extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("Login.fxml"));
        stage.initStyle(StageStyle.UNDECORATED);

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }

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

}

LoginController.java

LoginController.java

package skytravelsfx;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class LoginController implements Initializable {

    @FXML
    TextField username;

    @FXML
    TextField password;

    @FXML
    Button user;

    @FXML
    Button admin;

    @FXML
    Button login;

    @FXML
    public void btnClicked (ActionEvent actionEvent){
        try{
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("User.fxml"));
            Parent root=(Parent) fxmlLoader.load();
            Stage stage = new Stage();
            Scene scene = new Scene(root);
            stage.setScene(scene);
            stage.initStyle(StageStyle.UNDECORATED);
            stage.show();
           ((Stage)(((Button)actionEvent.getSource()).getScene().getWindow())).hide();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}

请注意我还没有写任何代码在UserController.java
我刚为User.fxml制作了ui

Please note that i still haven't written any codes in UserController.java I just made the ui for User.fxml

Login.fxml

Login.fxml

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

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

<AnchorPane id="AnchorPane" prefHeight="766.0" prefWidth="1366.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="skytravelsfx.LoginController">
    <children>
      <ImageView fitHeight="866.0" fitWidth="1397.0" layoutX="10.0" layoutY="11.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@../../../SkyTravelFx/src/goodwp.com_25168.jpg" />
         </image>
      </ImageView>
        <Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" />
      <Label fx:id="label1" layoutX="565.0" layoutY="517.0" minHeight="16" minWidth="69" text="Select User">
         <font>
            <Font size="18.0" />
         </font>
      </Label>
      <Rectangle arcHeight="5.0" arcWidth="5.0" fill="#095a77" height="140.0" stroke="BLACK" strokeType="INSIDE" strokeWidth="0.0" width="1386.0" />
      <Label fx:id="label11" layoutX="56.0" layoutY="33.0" minHeight="16" minWidth="69" text="Sky Travels" textFill="WHITE">
         <font>
            <Font name="System Italic" size="72.0" />
         </font>
      </Label>
      <Label fx:id="label12" layoutX="432.0" layoutY="91.0" minHeight="16" minWidth="69" text="Making your destination closer..." textFill="WHITE">
         <font>
            <Font name="System Italic" size="18.0" />
         </font>
      </Label>
      <AnchorPane layoutX="83.0" layoutY="252.0" prefHeight="507.0" prefWidth="383.0">
         <children>
            <ImageView fitHeight="191.0" fitWidth="234.0" layoutX="96.0" layoutY="6.0" pickOnBounds="true" preserveRatio="true">
               <image>
                  <Image url="@../../../SkyTravelFx/src/icon-default-profile.png" />
               </image>
            </ImageView>
              <Button fx:id="login" layoutX="153.0" layoutY="440.0" onAction="#handleButtonAction" style="-fx-background-color: #095a77;" text="Log In" textFill="WHITE">
               <font>
                  <Font size="18.0" />
               </font>
            </Button>
            <Label fx:id="label2" layoutX="152.0" layoutY="308.0" minHeight="16" minWidth="69" text="Password">
               <font>
                  <Font size="18.0" />
               </font>
            </Label>
            <Label fx:id="label2" layoutX="150.0" layoutY="214.0" minHeight="16" minWidth="69" text="Username">
               <font>
                  <Font size="18.0" />
               </font>
            </Label>
            <TextField fx:id="username" layoutX="115.0" layoutY="241.0" />
            <TextField fx:id="password" layoutX="115.0" layoutY="335.0" />
         </children>
      </AnchorPane>
      <Button fx:id="admin" layoutX="621.0" layoutY="592.0" mnemonicParsing="false" style="-fx-background-color: #095a77;" text="Log In as Admin" textFill="WHITE">
         <font>
            <Font size="18.0" />
         </font>
      </Button>
      <Button fx:id="user" layoutX="621.0" layoutY="675.0" mnemonicParsing="false" onAction="#btnClicked" prefHeight="39.0" prefWidth="153.0" style="-fx-background-color: #095a77;" text="Log In as User" textFill="WHITE">
         <font>
            <Font size="18.0" />
         </font>
      </Button>
    </children>
</AnchorPane>

这是我得到的运行时错误。

Here is the run-time error i am getting.


应用程序启动方法中的异常
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)
引起:java。 lang.RuntimeException:应用程序中的异常启动
方法
com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at
com.sun.javafx .application.LauncherImpl.lambda $ launchApplication $ 155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)引起:
javafx.fxml.LoadException:解决错误
onAction ='#handleButtonAction',事件处理程序不在
命名空间中,或者脚本中有错误。
file:/ C:/Users/Dell/Documents/NetBeansProjects/SkyTravelsFx/dist/run1245920268/SkyTravelsFx.jar!/skytravelsfx/Login.fxml:47

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$155(LauncherImpl.java:182) at java.lang.Thread.run(Thread.java:745) Caused by: javafx.fxml.LoadException: Error resolving onAction='#handleButtonAction', either the event handler is not in the Namespace or there is an error in the script. file:/C:/Users/Dell/Documents/NetBeansProjects/SkyTravelsFx/dist/run1245920268/SkyTravelsFx.jar!/skytravelsfx/Login.fxml:47

at
javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
at javafx.fxml.FXMLLoader.access $ 100(FXMLLoader.java:103)at
javafx.fxml.FXMLLoader $ Element.processEventHandlerAttributes(FXMLLoader.java:610)
at
javafx.fxml.FXMLLoader $ ValueElement.processEndElement(FXMLLoader.java:770)
at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader .java:2823)
javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532)at
javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)at
javafx。 fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)at
javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)at
javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148) at
javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)at
javafx.fxml.FXMLLoa der.loadImpl(FXMLLoader.java:3104)at
javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)at
skytravelsfx.SkyTravelsFx.start(SkyTravelsFx.java:23)at
com.sun.javafx.application.LauncherImpl.lambda $ launchApplication1 $ 162(LauncherImpl.java:863)
at
com.sun.javafx.application.PlatformImpl.lambda $ runAndWait $ 175(PlatformImpl.java: 326)
at
com.sun.javafx.application.PlatformImpl.lambda $ null $ 173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)at
com.sun.javafx.application.PlatformImpl.lambda $ runLater $ 174(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 $ 148(WinApplication.java :191)
... 1更多异常运行应用程序skytravelsfx.SkyTravelsFx
Java结果:1删除目录
C:\Users \Dell\Documents\NetBeansProjects \SkyTravelsFx \dist \run1245920268
jfxsa-run:BUILD SUCCESSFUL(总时间:4秒)

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597) at javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:103) at javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:610) at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:770) at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104) at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097) at skytravelsfx.SkyTravelsFx.start(SkyTravelsFx.java:23) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863) at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326) at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(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$148(WinApplication.java:191) ... 1 more Exception running application skytravelsfx.SkyTravelsFx Java Result: 1 Deleting directory C:\Users\Dell\Documents\NetBeansProjects\SkyTravelsFx\dist\run1245920268 jfxsa-run: BUILD SUCCESSFUL (total time: 4 seconds)


推荐答案

堆栈跟踪告诉您问题:

解析 onAction ='#handleButtonAction'时出错,要么事件处理程序不在命名空间中,要么脚本中有错误。

Error resolving onAction='#handleButtonAction', either the event handler is not in the Namespace or there is an error in the script.

你有

<Button fx:id="login" layoutX="153.0" layoutY="440.0" onAction="#handleButtonAction" ... >

但您的控制器没有名为 handleButtonAction的方法 。你可能想要

but your controller has no method called handleButtonAction. You probably want

<Button fx:id="login" layoutX="153.0" layoutY="440.0" onAction="#btnClicked" ... >

这篇关于Application start方法中的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆