A
A
Andrey Frolov2014-10-26 22:14:58
Java
Andrey Frolov, 2014-10-26 22:14:58

Why can't the constructor be called?

There is a Renderer class that implements the GLSurfaceView.Renderer interface, and there is a MyGLSurfaceView class that inherits GLSurfaceView. So in the MyGLSurfaceView constructor, it doesn't work to call the Renderer constructor. I try to follow the example from Google, but apparently I'm missing something. An error pops up Cannot instantiate the type GLSurfaceView.Renderer
Here is the actual code

public class MyGLSurfaceView extends GLSurfaceView {
  
  private final Renderer mRenderer;
  
   public MyGLSurfaceView(Context context) {
          super(context);

          // Create an OpenGL ES 2.0 context.
          setEGLContextClientVersion(2);

          // Set the Renderer for drawing on the GLSurfaceView
          mRenderer = new Renderer();
          setRenderer(mRenderer);

          // Render the view only when there is a change in the drawing data
          setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
      }
}

public class Renderer implements GLSurfaceView.Renderer {
  
  private Triangle mTriangle;
    private Square   mSquare;
  
  public static int loadShader(int type, String shaderCode){
    // create a vertex shader type (GLES20.GL_VERTEX_SHADER)
      // or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)
    int shader = GLES20.glCreateShader(type);
    
    // add the source code to the shader and compile it
    GLES20.glShaderSource(shader, shaderCode);
    GLES20.glCompileShader(shader);
    
    return shader;
  }
  
  @Override
  public void onDrawFrame(GL10 gl) {
     // Redraw background color
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
        mTriangle.Draw();
  }

  @Override
  public void onSurfaceChanged(GL10 gl, int width, int height) {
    GLES20.glViewport(0, 0, width, height);
  }

  @Override
  public void onSurfaceCreated(GL10 gl, EGLConfig config) {
      // Set the background frame color
        GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        
     // initialize a triangle
        mTriangle = new Triangle();
        // initialize a square
        mSquare = new Square();
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nonrblGyN4ik, 2014-10-26
@inham

Change the class that implements GLSurfaceView.Renderer to something else, like MyRender
Likely new Render(); trying to instantiate the GLSurfaceView.Renderer interface, look in Import to see if your class is being imported, not the interface.
The end result will be something like mRender = new MyRender();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question