gpt4 book ai didi

java - 错误: Exception in thread "JavaFX Application Thread"

转载 作者:行者123 更新时间:2023-12-02 03:38:46 24 4
gpt4 key购买 nike

我有一个程序,其中包括一艘宇宙飞船(大圆圈),它发射一颗子弹(小圆圈),过了一会儿就消失了。每次运行代码的特定部分时,它都会生成错误(但它会继续运行)。

所以我确定问题出在以下几行

private ArrayList<Bullet> bullets = new ArrayList<Bullet>();
//[...]
for (Bullet bullet : bullets) {
if (bullet.getLoops() > 50) {

bullets.remove(bullet); // specially in this line,
// because if I delete this, the error doesn't show up!
// I think it has something to do with the ArrayList.

} else {
bullet.next();
gc.strokeOval(bullet.getX() - 3, bullet.getY() - 3, 6, 6);
}
}

这是我得到的错误:

Exception in thread "JavaFX Application Thread" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
at java.util.ArrayList$Itr.next(ArrayList.java:851)
at sample.Main$4.handle(Main.java:131)
at javafx.animation.AnimationTimer$AnimationTimerReceiver.lambda$handle$484(AnimationTimer.java:57)
at java.security.AccessController.doPrivileged(Native Method)
at javafx.animation.AnimationTimer$AnimationTimerReceiver.handle(AnimationTimer.java:56)
at com.sun.scenario.animation.AbstractMasterTimer.timePulseImpl(AbstractMasterTimer.java:357)
at com.sun.scenario.animation.AbstractMasterTimer$MainLoop.run(AbstractMasterTimer.java:267)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:506)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:490)
at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$404(QuantumToolkit.java:319)
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)
at java.lang.Thread.run(Thread.java:745)

我很乐意根据要求提供所需的任何其他信息。

干杯。(抱歉我的英语不好,我来自德国,但几年前我在学校就开始学英语了。)

PS:如果有人需要主类的完整代码:

package sample;

import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;

import java.util.ArrayList;

public class Main extends Application {

private Player p = new Player(100, 100, 0);
private ArrayList<Bullet> bullets = new ArrayList<Bullet>();

private static boolean left;
private static boolean right;
private static boolean up;
private static boolean down;
private static boolean shoot;

@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
Group root0 = new Group();
Scene theScene = new Scene(root0, 500, 500);
primaryStage.setScene(theScene);
Canvas canvas = new Canvas(500, 500);
root0.getChildren().add(canvas);


theScene.setOnMouseMoved(
new EventHandler<MouseEvent>() {
public void handle(MouseEvent e) {
Vector a = new Vector(p.getX(), -p.getY());
Vector b = new Vector(e.getX(), -e.getY());
Vector c = new Vector(p.getX(), -p.getY() + 1);
p.setRotation(Vector.getAngle(a, b, c));
}
}
);
theScene.setOnKeyPressed(
new EventHandler<KeyEvent>() {
public void handle(KeyEvent e) {
String code = e.getCode().toString();
if (code.equals("A")) {
left = true;
}
if (code.equals("D")) {
right = true;
}
if (code.equals("W")) {
up = true;
}
if (code.equals("S")) {
down = true;
}
if (code.equals("SPACE")) {
if (!PlayerShot()) shoot = true;
}
}
}
);
theScene.setOnKeyReleased(
new EventHandler<KeyEvent>() {
public void handle(KeyEvent e) {
String code = e.getCode().toString();
if (code.equals("A")) {
left = false;
}
if (code.equals("D")) {
right = false;
}
if (code.equals("W")) {
up = false;
}
if (code.equals("S")) {
down = false;
}
if (code.equals("SPACE")) {
shoot = false;
}
}
}
);
GraphicsContext gc = canvas.getGraphicsContext2D();

Font theFont = Font.font("Helvetica", FontWeight.BOLD, 24);
gc.setFont(theFont);
gc.setStroke(Color.BLACK);
gc.setLineWidth(1);

new AnimationTimer() {
public void handle(long currentNanoTime) {
// Clear the canvas
gc.setFill(new Color(0.85, 0.85, 1.0, 1.0));
gc.fillRect(0, 0, 500, 500);

int x = p.getX();
int y = p.getY();
if (left && x >= 16) {
p.setX(x - 3);
}
if (right && x <= 500 - 16) {
p.setX(x + 3);
}
if (up && y >= 16) {
p.setY(y - 3);
}
if (down && y <= 500 - 16) {
p.setY(y + 3);
}
if (shoot) {
bullets.add(new Bullet(p.getX(), p.getY(), p.getRotationInVelocity(), p));
shoot = false;
}
gc.setStroke(Color.BLUE);
gc.strokeLine(p.getX(), p.getY(), p.getX() + Math.sin(p.getRotation()) * 16, p.getY() - Math.cos(p.getRotation()) * 16);
gc.strokeOval(p.getX() - 16, p.getY() - 16, 32, 32);
gc.setStroke(Color.RED);
for (Bullet bullet : bullets) {
if (bullet.getLoops() > 50) {
bullets.remove(bullet);
} else {
bullet.next();
gc.strokeOval(bullet.getX() - 3, bullet.getY() - 3, 6, 6);
}
}
}
}.start();

primaryStage.show();
}

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

// Hat der Spieler gerade eben schon geschossen?
public boolean PlayerShot() {
for (Bullet bullet : bullets) {
if(bullet.getShooter() == p)return true;
}
return false;
}
}

其他类只处理一些 int- 和 double-vars

最佳答案

你不能这么做。您将获得 ConcurrentModificationException

什么是ConcurrentModificationException

来自官方Javadoc :

This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible.

基本上,您正在迭代您的列表。如果您在某一时刻决定删除一个元素,这可能会导致不可预测的行为。为了避免这种情况,抛出此异常。

如何修复它?

只需使用 <a href="https://docs.oracle.com/javase/7/docs/api/java/util/Iterator.html" rel="noreferrer noopener nofollow">Iterator</a>而不是循环你的ArrayListIterator使用方法add(T element)remove(T element) .

关于java - 错误: Exception in thread "JavaFX Application Thread",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37104215/

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