2012-05-01 4 views
0

OpenGL 프로그래밍을 처음 접했고 간단한 드로잉을 단순화하여 드로잉을 단순화했습니다. 이제는 가속도계를 기반으로 변환을 만들고 싶습니다. ConcurrentModificationException이 표시됩니다.Android 및 OpenGL ES 1.1 ConcurrentModificationException

여기 객체 클래스의 :

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); 
    } 
} 

나는 moveDraw() 방법 for(IbnRushdTransformation trans: transformations)에 ConcurrentModificationException를 얻을.

아이디어가 있으십니까? 미리 감사드립니다!

답변

1

개체를 반복하면서 목록에서 개체를 제거 할 수 없습니다. 그것이 예외의 원인입니다.

for 루프 (transformations.iterator()) 대신 반복기를 사용하여 목록을 반복하는 경우 iterator.remove()를 호출하여 반복되는 동안 안전하게 제거 할 수 있습니다.

"반복 중에 컬렉션을 수정하는 유일한 안전한 방법은 Iterator.remove이며 반복 실행 중에 다른 방식으로 기본 컬렉션이 수정되면 동작이 지정되지 않습니다."

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