gpt4 book ai didi

启动方法中的 JavaFX InvokingTargetException

转载 作者:行者123 更新时间:2023-12-01 11:52:58 25 4
gpt4 key购买 nike

所以,我正在尝试启动我编写的 javafx 应用程序,但由于某种原因,很多事情都被破坏了。我正在尝试使用单例设计模式,但实际上我不认为这是问题所在。当我尝试获取 Display 类的实例时,代码中断。如果有人可以提供帮助,我将非常感激!我已经尝试了四个小时来调试这个...我通过添加公共(public)构造函数删除了单例模式,但这没有帮助。谢谢!

这是我的主要类(class):

public class Main extends Application {

@Override
public void start (Stage s) {
View view = new View(s);
view.init();
}

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

这是在 Main 中创建的 View 类:

public class View {
private Stage myStage;
private Model myModel;
private static final ResourceBundle myValues = ResourceBundle.getBundle(
"view.resources/display/values", new Locale("view"));
private Display myDisplay;
private String myResourcesLocation = "resources.languages/English";

public View(Stage s) {
myStage = s;
myModel = new Model();
}

public void init() {
myStage.setTitle(myValues.getString("Title"));
myDisplay = Display.getInstance(myStage, (Receiver) myModel);
Scene scene = myDisplay.getScene();
myStage.setScene(scene);
myStage.show();
}

}

这是从 View 调用的显示:

public class Display {
private static Display instance;
private Stage myStage;
private Scene myScene;
private BorderPane myRoot;
private MenuBar myMenuBar;
private Workspace myWorkspace;
private static Feed myFeed;
private static final ResourceBundle myValues = ResourceBundle.getBundle(
"resources/display/values", new Locale("display"));

public Display(Stage stage, Receiver myReceiver) {
myStage = stage;
myRoot = new BorderPane();
myRoot.setBottom(myFeed.getInstance(myReceiver));
myRoot.setTop(makeMenuBar());
// setupMenuBar();
myRoot.setCenter(makeWorkspace());
myScene = new Scene(myRoot, Integer.parseInt(myValues
.getString("Width")), Integer.parseInt(myValues
.getString("Height")));
myStage.setScene(myScene);
myStage.show();
}

protected static Display getInstance(Stage stage, Receiver myReceiver) {
if (instance == null)
instance = new Display(stage, myReceiver);
return instance;
}
private Node makeMenuBar() {

MenuBar menuBar = new MenuBar();
try {
menuBar.getMenus().add(makeMenu("File"));
menuBar.getMenus().add(makeMenu("Edit"));
menuBar.getMenus().add(makeMenu("View"));
menuBar.getMenus().add(makeMenu("Options"));
menuBar.getMenus().add(makeMenu("Help"));
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return menuBar;
}

private Menu makeMenu(String name) throws NoSuchMethodException,
IllegalAccessException, IllegalArgumentException,
InvocationTargetException {
Menu menu = new Menu(name);
String[] arrayCharles = myValues.getString(name).split(", ");
for (String s : arrayCharles) {
MenuItem item = new MenuItem();
Method m = Display.class.getDeclaredMethod(getMethodName(s));
// not sure why I'm getting this error
// item.setOnAction(e -> m.invoke(null, null));
menu.getItems().add(item);
}
return menu;
}

private String getMethodName(String s) {
s.replaceAll(" ", "");
String first = String.valueOf(s.charAt(0));
s.replaceFirst("%c", first.toLowerCase());
return s;
}

public Scene getScene() {
return this.myScene;
}


private Workspace makeWorkspace() {
return new Workspace();
}
}

最后,这是我收到的错误消息:

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:483)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:363)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:303)
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:483)
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:875)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(LauncherImpl.java:157)
at com.sun.javafx.application.LauncherImpl$$Lambda$49/1732398722.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ExceptionInInitializerError
at view.View.init(View.java:37)
at Main.start(Main.java:13)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
at com.sun.javafx.application.LauncherImpl$$Lambda$52/1459627066.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/1051754451.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/480204181.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/1775282465.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Caused by: java.util.MissingResourceException: Can't find bundle for base name resources/display/values, locale display
at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1564)
at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1387)
at java.util.ResourceBundle.getBundle(ResourceBundle.java:845)
at view.Display.<clinit>(Display.java:26)
... 12 more
Exception running application Main

最佳答案

问题出在您的程序尝试加载资源时。

private static final ResourceBundle myValues = ResourceBundle.getBundle(
"resources/display/values", new Locale("display"));

堆栈跟踪说明了一切:

Caused by: java.util.MissingResourceException: Can't find bundle for base name resources/display/values, locale display

关于启动方法中的 JavaFX InvokingTargetException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28650206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com