10

I would like to call a GLES20 method when an item from the options menu is selected.

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.clear:
            GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
            break;
        // ...
    }
}

This does not work since I am in the main thread and not in GLThread. It says:

call to OpenGL ES API with no current context (logged once per thread)

But what do I have to do to get things working?

1 Answer 1

23

I found the answer on my own:

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.clear:
            // GLSurfaceView.queueEvent
            surface.queueEvent(new Runnable() {
                @Override
                public void run() {
                    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
                }
            });
            break;
        // ...
    }
}
2
  • 1
    hi matthias,i get the same error on android 3.2.Could you tell when you faced this error with your application?
    – user831170
    Commented Sep 28, 2011 at 14:02
  • @RunO OpenGL and the main application are running in different threads. Therefore you cannot execute OpenGL commands from the main thread (see sourcecode in question). So, being in the main thread, you have to queue this command to the OpenGL thread (see sourcecode in my answer). I hope this helps. Otherwise start a new question, describe your problem, leave me a link in a comment and I'll see what I can do.
    – Matthias
    Commented Sep 28, 2011 at 15:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.