3

我正在浏览 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
4

1 回答 1

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);
    }
}
于 2013-01-07T18:32:56.267 回答