我遇到了这样的问题。当我在容器上调用.revalidate时,我得到了IndexOutOfBoundsException。
我有一个Runnable类,可以增加或减少组件的边距。我用它来制作一种抽屉动画。除了这个例外,一切都很完美。 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)
为什么会这样?可能是什么问题?
谢谢!
UPD: 这是我的runnable类:
删除了不必要的代码:
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。但是使用这个动画runnable调用Display.geInstance()。invokeAndBlock()可以按需要运行而不会抛出异常。
答案 0 :(得分:2)
您正在更改不在EDT上的UI,这可能会导致您正在运行的线程与EDT(事件调度线程)之间出现问题和冲突。