java - 尝试使用 AnchorPane 的扩展版本时的实例化和加载异常
问题描述
对于一个项目,我获得了代码并且必须将其重构为 FXML,我正在尝试使用我给出的一段代码,它是 AnchorPane 的扩展,但是当我在我的 FXML 中将它从 AnchorPane 切换到 World 时出现错误在场景生成器中以及当我运行它时。我已经尝试了很多东西,只是有点难过,不知道现在该尝试什么。我注释掉了 World 的原始构造函数,但如果它可能相关,可以发布它
世界:
package game;
import javafx.animation.AnimationTimer;
import javafx.scene.Node;
import javafx.scene.layout.AnchorPane;
import java.util.ArrayList;
import java.util.List;
public abstract class World extends AnchorPane {
private AnimationTimer timer;
public void createTimer() {
timer = new AnimationTimer() {
@Override
public void handle(long now) {
act(now);
List<Actor> actors = getObjects(Actor.class);
for (Actor anActor: actors) {
anActor.act(now);
}
}
};
}
public void start() {
createTimer();
timer.start();
}
public void stop() {
timer.stop();
}
public void add(Actor actor) {
getChildren().add(actor);
}
public void remove(Actor actor) {
getChildren().remove(actor);
}
public <A extends Actor> List<A> getObjects(Class<A> cls) {
ArrayList<A> someArray = new ArrayList<A>();
for (Node n: getChildren()) {
if (cls.isInstance(n)) {
someArray.add((A)n);
}
}
return someArray;
}
public abstract void act(long now);
}
FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import game.End?>
<?import game.Log?>
<?import game.WetTurtle?>
<?import game.Turtle?>
<?import game.Obstacle?>
<?import game.World?>
<World maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
prefHeight="800.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.2-internal"
xmlns:fx="http://javafx.com/fxml/1" fx:controller="game.Level1Controller">
<ImageView>
<image>
<Image url="@../backgrounds/frogbg.png" />
</image>
</ImageView>
<End layoutX="13.0" layoutY="138.8">
<image>
<Image url="@../end/End.png" />
</image></End>
<End layoutX="141.0" layoutY="138.0">
<image>
<Image url="@../end/End.png" />
</image>
</End>
<End layoutX="269.0" layoutY="138.0">
<image>
<Image url="@../end/End.png" />
</image>
</End>
<End layoutX="398.0" layoutY="138.0">
<image>
<Image url="@../end/End.png" />
</image>
</End>
<End layoutX="528.0" layoutY="138.0">
<image>
<Image url="@../end/End.png" />
</image>
</End>
<Log fitHeight="30.0" layoutY="209.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../log/log3.png" />
</image>
</Log>
<Log fitHeight="30.0" layoutX="220.0" layoutY="209.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../log/log3.png" />
</image>
</Log>
<Log fitHeight="30.0" layoutX="440.0" layoutY="209.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../log/log3.png" />
</image>
</Log>
<WetTurtle fitHeight="45.0" layoutX="600.0" layoutY="252.0" preserveRatio="true">
<image>
<Image url="@../turtle/TurtleAnimation1.png" />
</image>
</WetTurtle>
<WetTurtle fitHeight="45.0" layoutX="400.0" layoutY="252.0" preserveRatio="true">
<image>
<Image url="@../turtle/TurtleAnimation1.png" />
</image>
</WetTurtle>
<WetTurtle fitHeight="45.0" layoutX="200.0" layoutY="252.0" preserveRatio="true">
<image>
<Image url="@../turtle/TurtleAnimation1.png" />
</image>
</WetTurtle>
<Log fitHeight="35.0" layoutY="308.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../log/logs.png" />
</image>
</Log>
<Log fitHeight="35.0" layoutX="440.0" layoutY="308.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../log/logs.png" />
</image>
</Log>
<Log fitHeight="35.0" layoutX="50.0" layoutY="358.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../log/log3.png" />
</image>
</Log>
<Log fitHeight="35.0" layoutX="270.0" layoutY="358.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../log/log3.png" />
</image>
</Log>
<Log fitHeight="35.0" layoutX="490.0" layoutY="358.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../log/log3.png" />
</image>
</Log>
<Turtle fitHeight="45.0" layoutX="500.0" layoutY="402.0" preserveRatio="true">
<image>
<Image url="@../turtle/TurtleAnimation1.png" />
</image></Turtle>
<Turtle fitHeight="45.0" layoutX="300.0" layoutY="402.0" preserveRatio="true">
<image>
<Image url="@../turtle/TurtleAnimation1.png" />
</image>
</Turtle>
<WetTurtle fitHeight="45.0" layoutX="700.0" layoutY="402.0" preserveRatio="true">
<image>
<Image url="@../turtle/TurtleAnimation1.png" />
</image>
</WetTurtle>
<Obstacle fitHeight="28.0" layoutX="500.0" layoutY="511.0" preserveRatio="true">
<image>
<Image url="@../car/car1Left.png" />
</image>
</Obstacle>
<Obstacle fitHeight="35.0" layoutX="500.0" layoutY="557.0" preserveRatio="true">
<image>
<Image url="@../truck/truck2Right.png" />
</image>
</Obstacle>
<Obstacle fitHeight="35.0" layoutY="557.0" preserveRatio="true">
<image>
<Image url="@../truck/truck2Right.png" />
</image>
</Obstacle>
<Obstacle fitHeight="28.0" layoutX="550.0" layoutY="611.0" preserveRatio="true">
<image>
<Image url="@../car/car1Left.png" />
</image>
</Obstacle>
<Obstacle fitHeight="28.0" layoutX="400.0" layoutY="611.0" preserveRatio="true">
<image>
<Image url="@../car/car1Left.png" />
</image>
</Obstacle>
<Obstacle fitHeight="28.0" layoutX="250.0" layoutY="611.0" preserveRatio="true">
<image>
<Image url="@../car/car1Left.png" />
</image>
</Obstacle>
<Obstacle fitHeight="28.0" layoutX="100.0" layoutY="611.0" preserveRatio="true">
<image>
<Image url="@../car/car1Left.png" />
</image>
</Obstacle>
<Obstacle fitHeight="35.0" layoutX="600.0" layoutY="657.0" preserveRatio="true">
<image>
<Image url="@../truck/truck1Right.png" />
</image>
</Obstacle>
<Obstacle fitHeight="35.0" layoutX="300.0" layoutY="657.0" preserveRatio="true">
<image>
<Image url="@../truck/truck1Right.png" />
</image>
</Obstacle>
<Obstacle fitHeight="35.0" layoutY="657.0" preserveRatio="true">
<image>
<Image url="@../truck/truck1Right.png" />
</image>
</Obstacle>
</World>
错误
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:473)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:372)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:941)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:973)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:198)
at java.base/java.lang.Thread.run(Thread.java:844)
Caused by: javafx.fxml.LoadException:
/C:/Users/harry/IdeaProjects/Frogger%20Game/out/production/Frogger%20Game/game/fxml/level1.fxml:16
at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
at javafx.fxml/javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:105)
at javafx.fxml/javafx.fxml.FXMLLoader$InstanceDeclarationElement.constructValue(FXMLLoader.java:1021)
at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:754)
at javafx.fxml/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2722)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3253)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3210)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3179)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3152)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3129)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3122)
at Frogger.Game/game.Main.start(Main.java:25)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:919)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(PlatformImpl.java:449)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:175)
... 1 more
Caused by: java.lang.InstantiationException
at java.base/jdk.internal.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:488)
at java.base/java.lang.Class.newInstance(Class.java:560)
at javafx.fxml/javafx.fxml.FXMLLoader$InstanceDeclarationElement.constructValue(FXMLLoader.java:1019)
... 20 more
Exception running application game.Main
Process finished with exit code 1
解决方案
World
是抽象的,你不能构造一个抽象类的实例。
当你声明一个元素时:
<World ...>
World
在 FXML 中,当您加载 FXML 文档时,FXMLLoader 将尝试创建该类的实例。当它这样做时,它将失败,因为它World
是抽象的并且不能被实例化。
您的代码或设计中可能还有其他问题,但您至少有这里指出的问题。
如果我想创建它的一个实例但它扩展了 AnchorPane 我该怎么做?
你不能,你的类层次结构已经由你的扩展子句定义了,除非你改变层次结构设计(例如重新编码 World,所以它不是抽象的),你必须实例化 World 的一个具体子类。
我有另一个类扩展了非抽象的世界,所以我应该使用它来代替我猜?
是的。
推荐阅读
- javascript - 如果以编程方式运行 webpack,他找不到模块
- python - Pycharm 无法从父文件夹名称导入模块
- jekyll - Jekyll 没有阅读新帖子
- c# - 在 ASP.NET MVC 中刷新多个局部视图
- reactjs - 即使重新加载浏览器,如何使 localStorage 持续存在?
- c++ - 为什么在Friend函数中调用析构函数
- r - 如何在矩阵中插入%
- javascript - 如何将可水平滚动的 div 中的内容分成两行?
- unity3d - Unity 2019.3.9f1:使用TileMap.HasTile(),这正常吗?
- networking - MPI 中的发送和接收函数调用是否必然且始终涉及系统上的 IO?