避免索引越界异常 [英] Avoiding index out of bounds exceptions
问题描述
我使用一个数组列表存储有关屏幕上移动对象的数据。我怀疑这是因为我的渲染和我的逻辑是在单独的线程运行,但有时当数据被从列表中删除我得到一个IndexOutOfBoundsException异常。我已经采取了一切我能想到的措施来避免这种情况,包括try / catch语句,但异常仍时有发生。这是似乎导致异常我的渲染线程的一部分。
公共无效drawMobs(GL10 GL){
布尔播放= MyLaunchActivity.getPlaying();
如果(打==真){
尝试{
ArrayList的<串GT; mobDat = Play.getMobDat(); 而(currentLoadSpace< mobDat.size()){ 如果(mobDat.isEmpty()|| currentLoadSpace&下;!mobDat.size()){ 装入对象= mobDat.get(currentLoadSpace);
浮loadCoordX = Float.parseFloat(mobDat.get(currentLoadSpace + 2));
浮loadCoordY = Float.parseFloat(mobDat.get(currentLoadSpace + 3)); / *一些渲染* / currentLoadSpace + = 4;
}
}
}赶上(ArrayIndexOutOfBoundsException异常五){Log.d(TAG,中招);} currentLoadSpace = 0;}}
你可以看到我已经尝试了一些东西,但仍然出现错误。以下是错误日志
01-02 14:02:42.650:E / AndroidRuntime(6947):致命异常:GLThread 10
01-02 14:02:42.650:E / AndroidRuntime(6947):java.lang.IndexOutOfBoundsException:无效指数23,大小为20
01-02 14:02:42.650:E / AndroidRuntime(6947):在java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
01-02 14:02:42.650:E / AndroidRuntime(6947):在java.util.ArrayList.get(ArrayList.java:308)
01-02 14:02:42.650:E / AndroidRuntime(6947):在basicmelon.games.agameofsorts.LoadLevel.drawMobs(LoadLevel.java:530)
01-02 14:02:42.650:E / AndroidRuntime(6947):在basicmelon.games.agameofsorts.MyGLSurfaceRenderer.onDrawFrame(MyGLSurfaceRenderer.java:82)
01-02 14:02:42.650:E / AndroidRuntime(6947):在android.opengl.GLSurfaceView $ GLThread.guardedRun(GLSurfaceView.java:1429)
01-02 14:02:42.650:E / AndroidRuntime(6947):在android.opengl.GLSurfaceView $ GLThread.run(GLSurfaceView.java:1184)
一个可能的解决办法是:
的http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/CopyOnWriteArrayList.html
如果这不是一种选择,你可以使用同步(yourList){}至prevent concurent修改。
I am using an Array List to store data about moving objects on screen. I suspect this is because my renderer and my logic are running on separate threads, but sometimes when data is removed from the list I get an indexOutOfBoundsException. I have taken all the steps I can think of to avoid this including try/catch but the exception still sometimes occurs. This is the part of my renderer thread that seems to cause the exception.
public void drawMobs(GL10 gl) {
boolean playing = MyLaunchActivity.getPlaying();
if(playing == true){
try{
ArrayList<String> mobDat = Play.getMobDat();
while (currentLoadSpace < mobDat.size()){
if(!mobDat.isEmpty() || currentLoadSpace < mobDat.size()){
loadObject = mobDat.get(currentLoadSpace);
float loadCoordX = Float.parseFloat(mobDat.get(currentLoadSpace + 2));
float loadCoordY = Float.parseFloat(mobDat.get(currentLoadSpace + 3));
/*some rendering*/
currentLoadSpace+=4;
}
}
}catch( ArrayIndexOutOfBoundsException e){ Log.d(TAG, "caught");}
currentLoadSpace = 0;
}}
as you can see I have tried a few things but the error still occurs. Here is the error log
01-02 14:02:42.650: E/AndroidRuntime(6947): FATAL EXCEPTION: GLThread 10
01-02 14:02:42.650: E/AndroidRuntime(6947): java.lang.IndexOutOfBoundsException: Invalid index 23, size is 20
01-02 14:02:42.650: E/AndroidRuntime(6947): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
01-02 14:02:42.650: E/AndroidRuntime(6947): at java.util.ArrayList.get(ArrayList.java:308)
01-02 14:02:42.650: E/AndroidRuntime(6947): at basicmelon.games.agameofsorts.LoadLevel.drawMobs(LoadLevel.java:530)
01-02 14:02:42.650: E/AndroidRuntime(6947): at basicmelon.games.agameofsorts.MyGLSurfaceRenderer.onDrawFrame(MyGLSurfaceRenderer.java:82)
01-02 14:02:42.650: E/AndroidRuntime(6947): at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1429)
01-02 14:02:42.650: E/AndroidRuntime(6947): at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1184)
One possible solution could be:
http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/CopyOnWriteArrayList.html
If this is not an option, you could use synchronized(yourList) {} to prevent concurent modifications.
这篇关于避免索引越界异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!