gpt4 book ai didi

java - 代号一container.revalidate()有时会抛出IndexOutOfBoundsException

转载 作者:行者123 更新时间:2023-12-02 05:24:57 25 4
gpt4 key购买 nike

我就遇到过这样的问题。当我在容器上调用 .revalidate 时,出现 IndexOutOfBoundsException。

我有一个 Runnable 类,它可以增加或减少组件的边距。我用它来制作抽屉动画。除了这个异常(exception)之外,一切都运行良好。 Ans 异常是随机抛出的。在将此异常记录到控制台之前,我可以多次运行“动画”方法。

在该异常之后,应用程序运行正常。仅容器显示不正确。

这是错误日志:

Exception in thread "Thread-15" java.lang.IndexOutOfBoundsException: Index: 5, Size: 1
at java.util.ArrayList.rangeCheck(ArrayList.java:635)
at java.util.ArrayList.get(ArrayList.java:411)
at com.codename1.ui.Container.getComponentAt(Container.java:1083)
at com.codename1.ui.Container.doLayout(Container.java:1054)
at com.codename1.ui.Container.layoutContainer(Container.java:1043)
at com.codename1.ui.Container.doLayout(Container.java:1056)
at com.codename1.ui.Container.layoutContainer(Container.java:1043)
at com.codename1.ui.Container.doLayout(Container.java:1056)
at com.codename1.ui.Container.layoutContainer(Container.java:1043)
at com.codename1.ui.Container.doLayout(Container.java:1056)
at com.codename1.ui.Container.layoutContainer(Container.java:1043)
at com.codename1.ui.Container.revalidate(Container.java:835)
at util.classes.AnimationRunnable.run(AnimationRunnable.java:75)
at java.lang.Thread.run(Thread.java:745)

为什么会这样?可能是什么问题?

谢谢!

更新:这是我的可运行类:

删除了不必要的代码:

public class AnimationRunnable implements Runnable {
private float marginFrom = 0;
private float marginTo = 0;
private Container bottomContainer;
private int screenHeight;

public AnimationRunnable(Container bottomContainer, int screenHeight, float marginFrom, float marginTo) {
this.marginFrom = marginFrom;
this.marginTo = marginTo;
this.bottomContainer = bottomContainer;
this.screenHeight = screenHeight;
}

@Override
public void run() {
boolean finished = false;
boolean directionUp = marginFrom > marginTo; // true = up; false = down
Container parent = bottomContainer.getParent();

while (!finished) {
// if distance between current margin and destination margin is < 10, finish
if (Math.abs(marginFrom - marginTo) < 10) {
finished = true;
} else {
// if container size is increasing
if (directionUp) {
// if current margin is less than destination margin
if (marginFrom <= marginTo) {
marginFrom = marginTo;
finished = true;
} else {
// else - modify current margin to be closer to destination margin
marginFrom -= Math.ceil((marginFrom - marginTo) / 6);
}
} else {
// if current margin is less than destination margin
if (marginFrom >= marginTo) {
marginFrom = marginTo;
finished = true;
} else {
// else - modify current margin to be closer to destination margin
marginFrom += Math.ceil((marginTo - marginFrom) / 6);
}
}
}

// Setting style to container (in case if container's current style is changed)
Style defaultStyle = bottomContainer.getStyle();
bottomContainer.getStyle().setMargin(Component.TOP, Math.round(marginFrom));
// Setting default style to all container states

// HERE EXCEPTION IS THROWN
// Revalidating either parent, or container itself
if (parent != null) {
parent.revalidate();
parent.repaint();
} else {
bottomContainer.revalidate();
bottomContainer.repaint();
}

try {
// Wait 25ms to make animation smooth
Thread.sleep(25);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

// revalidate once more after loop is over
if (parent != null) {
parent.revalidate();
parent.repaint();
} else {
bottomContainer.revalidate();
bottomContainer.repaint();
}
return;
}
}

更新

CallSerially 不显示动画,它只是等待,并在动画可运行时更新 UI。但是使用此动画可运行调用 Display.geInstance().invokeAndBlock() 可以按需要正常工作,不会引发异常。

最佳答案

您正在更改 UI,而不是在 EDT 上,这可能会导致您正在运行的线程与 EDT(事件调度线程)之间出现问题和冲突。

关于java - 代号一container.revalidate()有时会抛出IndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26139205/

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