24

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.

TATN
  • 1,249
  • 2
  • 12
  • 26
  • About "Source not Found". Look this topic: http://stackoverflow.com/questions/14942851/the-source-attachment-does-not-contain-the-source-for-the-file-layoutinflater-cl –  Oct 09 '14 at 09:50
  • About "Source not found" error message. Look this topic: http://stackoverflow.com/questions/14942851/the-source-attachment-does-not-contain-the-source-for-the-file-layoutinflater-cl –  Oct 09 '14 at 09:57

1 Answers1

42

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);
}
TATN
  • 1,249
  • 2
  • 12
  • 26
  • 1
    As part of sorting out the Android app framework and OpenGL ES 2 I wrote a Breakout game using GLSurfaceView. It goes well past the code in the framework docs/demos but doesn't do anything especially crazy, so it should still work as demo code. You can find it at http://code.google.com/p/android-breakout/ . – fadden Nov 12 '12 at 19:10