Android OpenGL ES Tutorial

I look at the tutorial on the Android developer’s site, and I have run the creation of the environment several times without getting a gray screen, which should indicate in the lesson.

Link to the corresponding tutorial: http://developer.android.com/training/graphics/opengl/environment.html

The program seems to compile and install just fine, but when it starts, it "closes unexpectedly."

Here is my code ... Main.java:

package com.wiley.openglplayground; import android.app.Activity; import android.content.Context; import android.opengl.GLSurfaceView; import android.os.Bundle; public class Main extends Activity { private GLSurfaceView mGLView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mGLView = new GLSurfaceView(this); setContentView(mGLView); } class MyGLSurfaceView extends GLSurfaceView { public MyGLSurfaceView(Context context) { super(context); setEGLContextClientVersion(2); setRenderer(new MyRenderer()); setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); } } } 

MyRenderer.java:

 package com.wiley.openglplayground; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; import android.opengl.GLES20; import android.opengl.GLSurfaceView; public class MyRenderer implements GLSurfaceView.Renderer { @Override public void onDrawFrame(GL10 gl) { GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); } @Override public void onSurfaceChanged(GL10 gl, int width, int height) { GLES20.glViewport(0, 0, width, height); } @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1.0f); } } 

From what I can say, the code should work as it would be in them in the textbook ... What am I missing? Can't I start it at this point?

Thanks guys!

On request, this is the result of LogCat:

 12-27 14:27:05.768: W/ActivityThread(3493): Application com.wiley.openglplayground is waiting for the debugger on port 8100... 12-27 14:27:05.778: I/System.out(3493): Sending WAIT chunk 12-27 14:27:05.878: I/dalvikvm(3493): Debugger is active 12-27 14:27:05.978: I/System.out(3493): Debugger has connected 12-27 14:27:05.978: I/System.out(3493): waiting for debugger to settle... 12-27 14:27:06.179: I/System.out(3493): waiting for debugger to settle... 12-27 14:27:06.379: I/System.out(3493): waiting for debugger to settle... 12-27 14:27:06.579: I/System.out(3493): waiting for debugger to settle... 12-27 14:27:06.779: I/System.out(3493): waiting for debugger to settle... 12-27 14:27:06.979: I/System.out(3493): waiting for debugger to settle... 12-27 14:27:07.180: I/System.out(3493): waiting for debugger to settle... 12-27 14:27:07.380: I/System.out(3493): waiting for debugger to settle... 12-27 14:27:07.580: I/System.out(3493): debugger has settled (1420) 12-27 14:27:07.890: D/libEGL(3493): loaded /system/lib/egl/libGLES_android.so 12-27 14:27:07.890: D/libEGL(3493): loaded /system/lib/egl/libEGL_adreno200.so 12-27 14:27:07.900: D/libEGL(3493): loaded /system/lib/egl/libGLESv1_CM_adreno200.so 12-27 14:27:07.900: D/libEGL(3493): loaded /system/lib/egl/libGLESv2_adreno200.so 12-27 14:27:07.950: D/OpenGLRenderer(3493): Enabling debug mode 0 12-27 14:27:08.711: D/dalvikvm(3493): threadid=1: still suspended after undo (sc=1 dc=1) 12-27 14:28:21.132: D/AndroidRuntime(3493): Shutting down VM 12-27 14:28:21.132: W/dalvikvm(3493): threadid=1: thread exiting with uncaught exception (group=0x40a601f8) 12-27 14:28:21.252: E/AndroidRuntime(3493): FATAL EXCEPTION: main 12-27 14:28:21.252: E/AndroidRuntime(3493): java.lang.NullPointerException 12-27 14:28:21.252: E/AndroidRuntime(3493): at android.opengl.GLSurfaceView.surfaceCreated(GLSurfaceView.java:512) 12-27 14:28:21.252: E/AndroidRuntime(3493): at android.view.SurfaceView.updateWindow(SurfaceView.java:533) 12-27 14:28:21.252: E/AndroidRuntime(3493): at android.view.SurfaceView.access$000(SurfaceView.java:81) 12-27 14:28:21.252: E/AndroidRuntime(3493): at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:169) 12-27 14:28:21.252: E/AndroidRuntime(3493): at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:590) 12-27 14:28:21.252: E/AndroidRuntime(3493): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1617) 12-27 14:28:21.252: E/AndroidRuntime(3493): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442) 12-27 14:28:21.252: E/AndroidRuntime(3493): at android.os.Handler.dispatchMessage(Handler.java:99) 12-27 14:28:21.252: E/AndroidRuntime(3493): at android.os.Looper.loop(Looper.java:137) 12-27 14:28:21.252: E/AndroidRuntime(3493): at android.app.ActivityThread.main(ActivityThread.java:4575) 12-27 14:28:21.252: E/AndroidRuntime(3493): at java.lang.reflect.Method.invokeNative(Native Method) 12-27 14:28:21.252: E/AndroidRuntime(3493): at java.lang.reflect.Method.invoke(Method.java:511) 12-27 14:28:21.252: E/AndroidRuntime(3493): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 12-27 14:28:21.252: E/AndroidRuntime(3493): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556) 12-27 14:28:21.252: E/AndroidRuntime(3493): at dalvik.system.NativeStart.main(Native Method) 12-27 14:28:21.442: I/Process(3493): Sending signal. PID: 3493 SIG: 9 
+4
source share
1 answer

Ok, I figured it out.

I realized that when I initialized the mGLView variable, I initialized it with a new GLSurfaceView instead of * My * GLSurfaceView.

Fixed Code:

 package com.wiley.opengles; import android.app.Activity; import android.content.Context; import android.opengl.GLSurfaceView; import android.os.Bundle; public class MainActivity extends Activity { public GLSurfaceView mGLView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mGLView = new MyGLSurfaceView(this); //This is where the problem was. setContentView(mGLView); } } class MyGLSurfaceView extends GLSurfaceView { public MyGLSurfaceView(Context context) { super(context); setEGLContextClientVersion(2); setRenderer(new MyRenderer()); setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); } } 
+2
source

All Articles