我正在浏览Android开发者网站上的教程,现在我已经多次构建环境,但没有得到教程指示应该是结果的“灰色屏幕”。
链接到相关教程: http://developer.android.com/training/graphics/opengl/environment.html
该程序似乎编译和安装就好了,但是当它运行时它“意外关闭。”
这是我的代码...... 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);
}
}
据我所知,代码应该像教程中的代码一样工作......我缺少什么?我不应该在这个阶段运行吗?
谢谢你们!
每个请求,这是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
答案 0 :(得分:2)
好吧,我明白了。
我意识到当我初始化mGLView变量时,我使用新的GLSurfaceView而不是* My * GLSurfaceView初始化它。
固定代码:
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);
}
}