2015-04-08 14 views
13

Tôi đang gặp một số sự cố khi tải ứng dụng Hello World đơn giản để chạy. Người ta ném các lỗi sau:Lỗi khi giải quyết "onAction" khi đang tải FXML

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/1329552164.run(Unknown Source) 
    at java.lang.Thread.run(Thread.java:745) 
Caused by: javafx.fxml.LoadException: Error resolving onAction='#printHello', either the event handler is not in the Namespace or there is an error in the script. 
/home/willi/java/IdeaProjects/JavaFXApp/out/production/JavaFXApp/sample/sample.fxml:23 

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601) 
    at javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:104) 
    at javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:606) 
    at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:766) 
    at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2827) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2536) 
    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$57/2085742749.run(Unknown Source) 
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326) 
    at com.sun.javafx.application.PlatformImpl$$Lambda$52/738441956.run(Unknown Source) 
    at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295) 
    at com.sun.javafx.application.PlatformImpl$$Lambda$55/689939224.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$53/1382865734.run(Unknown Source) 
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) 
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method) 
    at com.sun.glass.ui.gtk.GtkApplication.lambda$null$48(GtkApplication.java:139) 
    at com.sun.glass.ui.gtk.GtkApplication$$Lambda$43/704249306.run(Unknown Source) 
    ... 1 more 

tập tin của tôi Controller.java có phương pháp printHelloprintWorld quy định tại họ, và tôi muốn gọi cho họ khi tôi nhấn nút tương ứng của họ. Mã như sau: mẫu gói;

import javafx.fxml.FXML; 
import javafx.scene.control.Button; 
import java.awt.event.ActionEvent; 

public class Controller { 

    @FXML 
    private Button helloButton; 

    @FXML 
    private Button worldButton; 

    @FXML 
    private void printHello(ActionEvent e){ 
     System.out.println("Hello"); 
    } 

    @FXML 
    private void printWorld(ActionEvent f){ 
     System.out.println("World"); 
    } 
} 

`Trong tệp fxml, tôi có hai nút được xác định. Mã của họ như sau:

<Button fx:id="helloButton" layoutX="46.0" layoutY="87.0" mnemonicParsing="false" onAction="#printHello" text="Hello" /> 

<Button fx:id="worldButton" layoutX="97.0" layoutY="87.0" mnemonicParsing="false" onAction="#printWorld" text="World" /> 

Tại sao mã của tôi lại ném lỗi này?

Xin lỗi trước vì định dạng khủng khiếp, đây là lần đầu tiên tôi đăng bài.

+0

Xin vui lòng gửi mã thực tế bạn đang cố gắng để chạy. – mjuarez

+0

Điều này sẽ hoạt động. Bạn có thể đảm bảo rằng bạn đã cung cấp tên lớp điều khiển chính xác trong fxml không? Hoặc, đăng fxml hoàn chỉnh tại đây, cùng với tên gói cho Bộ điều khiển. – ItachiUchiha

Trả lời

39

Bạn đã nhập sai ActionEvent trong lớp bộ điều khiển.

import java.awt.event.ActionEvent; 

Đúng nó để

import javafx.event.ActionEvent; 
Các vấn đề liên quan