Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGLES on Android - IllegalStateException: setRenderer has already been called for this instance

Tags:

I'm new to OpenGL-ES on Android, so excuse me for my noobish question. I'm building this program for Android v2.2 - SDK#8. My tablet supports up to Android v3.1

I'm trying to setup an OpenGL-ES environment for Android by following a tutorial on developer.android.com. The program compiled fine, and it was supposed to display a simple blue screen on the device. However, when I tried to run it on my Android device, I got the "IllegalStateException: setRenderer has already been called for this instance" error.

Below is my code:

public class TA_SpaceActivity extends Activity  {     private MyGLSurfaceView myGLView;      @Override     public void onCreate(Bundle savedInstanceState)     {         super.onCreate(savedInstanceState);         myGLView = new MyGLSurfaceView(this); //NOTE: this is where the app crashed         setContentView(myGLView);     } }  class MyGLSurfaceView extends GLSurfaceView {     public MyGLSurfaceView(Context context)      {         super(context);         setRenderer (new MyRenderer());         setEGLContextClientVersion(2);         setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);     } }  class MyRenderer implements GLSurfaceView.Renderer {     public void onSurfaceCreated(GL10 unsued, EGLConfig config)     {         GLES20.glClearColor(0.0f, 0.0f, 1.0f, 1.0f);     }      public void onDrawFrame(GL10 unused)     {         GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);     }      public void onSurfaceChanged(GL10 unused, int width, int height)     {         GLES20.glViewport(0, 0, width, height);     } }   

First, I made sure that the uses-feature tag for OpenGLES was included in the AndroidManifest.xml file:

enter image description here

Then, when I did a Debug run, the ActivityThread.perfo showed a "Source Not Found" error message. So, I added the path to it (and I also made sure that the android.jar file exists in the directory)

enter image description here

Still, the app crashed right at the line "myGLView = new MyGLSurfaceView(this)". When I examined the LogCat, it showed that the program threw an IllegalStateException at the setRenderer() function call.

enter image description here

So, I have 2 puzzles that I don't understand at the moment:

1) Why did it throw a "Source Not Found" error message when the link to the source was clearly defined in the project?

2) Why did it say "setRenderer() has been called for this instance"? I only called it once in my "MyGLSurfaceView" subclass.

For the 1st puzzle, from what I heard, Eclipse will almost always throw a "Source Not Found" message when for every random error you make. Is it right? (if not, please correct me).

If this is the case, then I think the root cause of the issue has something to do with the setRenderer() method in my subclass. After the whole day messing around, I couldn't find a way to fix this issue. Would any body give me some pointer of what I can try to fix this "IllegalStateException: setRenderer() has been called for this instance" problem?

Thank you in advance for your help.

like image 245
TATN Avatar asked Nov 09 '12 02:11

TATN


1 Answers

I figured it out. After digging into Google's documentations, I found out that setEGLContextClientVersion() called the checkRenderThreadState(); this function throws the illegal exception "setRenderer() has been called for this instance" if the setRenderer() had been called. So, instead of calling setRenderer() first, I called the setEGLContextClientVersion() first, and the program compiled and ran without a problem. I see the beautiful blue screen showing up on my device now.

Here's the change that I made:

public MyGLSurfaceView(Context context)  {     super(context);     setEGLContextClientVersion(2);     setRenderer (new MyRenderer());     setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); } 
like image 126
TATN Avatar answered Oct 06 '22 14:10

TATN