2017-07-10 45 views
0

我使用IDEA Intellij。Javafx加載xml文件奇怪的錯誤

我有一個問題,我可以加載某些XML文件,但其他人不能。

例如,我可以加載gameFrame和示例fxml文件。

sample fxml loaded

但沒有啓動畫面或的MainView FXML文件

MainView not being able to load

一些彈出是例外的: 的InvocationTargetException RuntimeException的:異常在應用程序啓動方法 javafx.fxml。 LoadException ClassNotFoundException:view.MainView

當我點擊問題的源頭時,它會指引我:startLayout = loader.load();行,當我在啓動方法中調用它。

重建程序並不能解決問題。 我看到一些網站提到有關資源文件沒有被提出的東西?那可能是?如果是的話,有人可以更多地瞭解它。

下面是代碼:

package sample; 

import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Scene; 
import javafx.scene.layout.Pane; 
import javafx.stage.Stage; 
import java.io.IOException; 

public class Main extends Application { 

private Pane startLayout, gameFrame; 
private Stage primaryStage; 

// 
@Override 
public void start(Stage primaryStage) throws Exception { 
    this.primaryStage = primaryStage; 
    this.primaryStage.setTitle("Ron"); 

    showStartScreen(); 
} 
// 

public void showStartScreen() throws IOException { 

    FXMLLoader loader = new FXMLLoader(); 
    loader.setLocation(Main.class.getResource("/view /mainView.fxml")); 
    startLayout = loader.load(); 
    Scene scene = new Scene(startLayout); 
    primaryStage.setScene(scene); 
    primaryStage.show(); 


} 


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

錯誤消息:

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:18 2) 
at java.lang.Thread.run(Thread.java:748) 
Caused by: javafx.fxml.LoadException: 
/C:/Users/Quy%20Phan/IdeaProjects/Ronv2/out/production/Ronv2/view/mainView.fxml:12 

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601) 
at javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:103) 
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:922) 
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971) 
at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220) 
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744) 
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707) 
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527) 
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441) 
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409) 
at sample.Main.showStartScreen(Main.java:42) 
at sample.Main.start(Main.java:34) 
at  com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:8 63) 
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 
Caused by: java.lang.ClassNotFoundException: view.MainView 
at java.net.URLClassLoader.findClass(URLClassLoader.java:381) 
at java.lang.ClassLoader.loadClass(ClassLoader.java:424) 
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) 
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) 
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:920) 
... 18 more 
Exception running application sample.Main 

Process finished with exit code 1 
+1

發佈完整的堆棧跟蹤。 –

回答

0

根據您的堆棧跟蹤而載入mainView.fxml,並通過在該文件的第12行的原因導致出現異常。如果你可以發佈文件的內容,我可以更清楚地回答。

這應該可能是一個評論,但我是新來的,沒有足夠的聲譽......對不起。

+0

<?xml version =「1.0」encoding =「UTF-8」?> <?import java.lang。*?> <?import java.util。*?> <?import javafx.scene。* ?> <?import javafx.scene.control。*?> <?import javafx.scene.layout。*?>

+0

在view.MainView 所述的MainView類被突出顯示紅色 –

+0

說不能解決ve class MainView –