2012-05-01 165 views
0

我很新的OpenGL编程,我做了一个简单的引擎来简化我的图纸,它的工作。现在我想基于加速度计进行转换,并且我得到一个ConcurrentModificationExceptionConcurrentModificationException与Android和OpenGL ES 1.1

这里的对象类:

public class IbnRushdObject 
{ 
    /** The graphical object to show */ 
    private Mesh mesh = null; 

    /** List of transformations to be executed upon this object */ 
    private List<IbnRushdTransformation> transformations; 

    /** Lock to prevent modification of the list when executing the transformations and viceversa */ 
    private final ReentrantLock lock = new ReentrantLock(); 

    /** 
    * Initializes this IbnRushd object with a mesh 
    * @param mesh 
    */ 
    public IbnRushdObject(Mesh mesh) 
    { 
     this.mesh = mesh; 
    } 

    /** 
    * Adds a transformation to be performed on this object<br> 
    * The transformation does not take place until {@link #moveDraw(GL10)} is called 
    * @param trans 
    */ 
    public void addTransformation(IbnRushdTransformation trans) 
    { 
     try 
     { 
      lock.lock(); 
      if (transformations == null) 
      { 
       transformations = new LinkedList<IbnRushdTransformation>();   
      } 
      transformations.add(trans); 
     } 
     finally 
     { 
      lock.unlock(); 
     } 
    } 

    /** 
    * Executes transformations for this object and draws it 
    * @param gl 
    */ 
    public void moveDraw(GL10 gl) 
    { 
     gl.glMatrixMode(GL10.GL_MODELVIEW); 
     gl.glLoadIdentity(); 

     try 
     { 
      lock.lock(); 
      for(IbnRushdTransformation trans: transformations) // ConcurrentMoificationException thrown here 
      { 
       trans.execute(gl); 
       if (!trans.isPermanent()) 
       { 
        transformations.remove(trans); 
       } 
      } 
     } 
     finally 
     { 
      lock.unlock(); 
     } 

     gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); 
     gl.glEnableClientState(GL10.GL_COLOR_ARRAY); 

     mesh.draw(gl); 
    } 
} 

而且moveDraw()方法只从

@Override 
public void onDrawFrame(GL10 gl) 
{ 
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); 
    gl.glClearColor(0, 0.5f, 0.5f, 1.0f); 

    for(IbnRushdObject object : objectQueue) 
    { 
     object.moveDraw(gl); 
    } 
} 

它包含所有对象的列表绘制调用。

这是侦听加速度计事件的代码。监听器方法是onEventChange()

public class IbnRushdOrientation implements ServiceListener<Orientation> 
{ 
    private IbnRushdObject object = null; 

    public IbnRushdOrientation(IbnRushdObject obj) 
    { 
     object = obj; 
    } 

    @Override 
    public void onEventChange(Orientation arg0) 
    { 
     IbnRushdRotation hrot = new IbnRushdRotation(); 
     hrot.setFixed(0, 0, 1, 0); 
     hrot.setIncremental((float)arg0.getHorizontalAngle()); 
     hrot.setPermanent(false); 

     IbnRushdRotation vrot = new IbnRushdRotation(); 
     vrot.setFixed(0, 1, 0, 0); 
     vrot.setIncremental((float)arg0.getVerticalAngle()); 
     vrot.setPermanent(false); 

     object.addTransformation(hrot); 
     object.addTransformation(vrot); 
    } 
} 

我得到for(IbnRushdTransformation trans: transformations)的ConcurrentModificationException的在moveDraw()方法

任何想法?提前致谢!

回答

1

当您迭代它时,无法从列表中删除对象。这就是导致这种异常的原因。

如果使用迭代器而不是for循环(transformations.iterator())迭代列表,那么可以在迭代时调用iterator.remove()以安全地删除它。

“请注意,Iterator.remove是在迭代期间修改集合的唯一安全方法;如果在迭代过程中以任何其他方式修改了基础集合,则行为未指定。

http://java.sun.com/docs/books/tutorial/collections/interfaces/collection.html