当我尝试创建Opengl ES环境时,我的应用程序不断崩溃

时间:2014-06-23 15:54:14

标签: java android eclipse opengl-es-2.0

我正在关注如何使用图形创建自己的Open GL ES环境的android教程。除了看起来好像教程正在跳过代码而他们假设我们知道要包含的事实这一事实我只是让我的应用程序保持打开而没有错误消息,“不幸的是,MyApp已停止”五秒钟之后该应用程序打开。我不相信它是导致这个的三角类,因为我注释掉了三角形对象并试图再次运行它,但同样的事情发生了。我还包括了应该包含在清单中的必要代码。

这是我的代码(Open GL ES代码在虚线之间):

    public class MainActivity extends ActionBarActivity {

private GLSurfaceView mGLView;
private GLSurfaceView.Renderer MyRenderer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mGLView=new MyGLSurfaceView(this);
    setContentView(mGLView);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        return rootView;
    }
}

//....................................................................................................
class MyGLSurfaceView extends GLSurfaceView{

    public MyGLSurfaceView(Context context){
        super(context);
        MyRenderer=new MyGlRenderer();
        setRenderer(MyRenderer);
        setEGLContextClientVersion(2);
        setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
    }
}

//Triangle mTriangle;

public class MyGlRenderer implements GLSurfaceView.Renderer{
    @Override
    public void onSurfaceCreated(GL10 unused, EGLConfig config){
        GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

        //mTriangle= new Triangle();
    }
    @Override
    public void onDrawFrame(GL10 unused){

        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
        //mTriangle.draw();
    }
    @Override
    public void onSurfaceChanged(GL10 unused, int width, int height){
        GLES20.glViewport(0,0,width,height);
    }

}

public class Triangle{

    private FloatBuffer vertexBuffer;
    static final int COORDS_PER_VERTEX=3;
    float triangleCoords[]={
        0.0f, 0.622008459f, 0.0f,//top
        -0.5f, -0.311004243f, 0.0f, //bottom left
        0.5f, -0.311004242f,0.0f//bottom right
    };

    int mProgram;
    float color[] = { 0.63671875f, 0.76953125f, 0.22265625f, 1.0f };

    private final String vertexShaderCode =
            "attribute vec4 vPosition;" +
            "void main() {" +
            "  gl_Position = vPosition;" +
            "}";

        private final String fragmentShaderCode =
            "precision mediump float;" +
            "uniform vec4 vColor;" +
            "void main() {" +
            "  gl_FragColor = vColor;" +
            "}";

    public Triangle(){
        ByteBuffer bb=ByteBuffer.allocateDirect(
                triangleCoords.length*4);
        bb.order(ByteOrder.nativeOrder());
        vertexBuffer=bb.asFloatBuffer();
        vertexBuffer.put(triangleCoords);
        vertexBuffer.position(0);

        int vertexShader=loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
        int fragmentShader=loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);

        mProgram=GLES20.glCreateProgram();
        GLES20.glAttachShader(mProgram, vertexShader);
        GLES20.glAttachShader(mProgram, fragmentShader);
        GLES20.glLinkProgram(mProgram);

    }

    public int loadShader(int type, String shaderCode){
        int shader=GLES20.glCreateShader(type);
        GLES20.glShaderSource(shader, shaderCode);
        GLES20.glCompileShader(shader);
        return shader;
    }

    static final int vertexStride = COORDS_PER_VERTEX * 3;
    static final int vertexCount = 3;


    public void draw(){
        GLES20.glUseProgram(mProgram);
        int mPositionHandle= GLES20.glGetAttribLocation(mProgram, "vPosition");
        GLES20.glEnableVertexAttribArray(mPositionHandle);
        GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX,
                                     GLES20.GL_FLOAT, false,
                                      vertexStride, vertexBuffer);
        int mColorHandle=GLES20.glGetUniformLocation(mProgram,"vColor");
        GLES20.glUniform4fv(mColorHandle, 1, color,0);
        GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);
        GLES20.glDisableVertexAttribArray(mPositionHandle);
    }
}
//....................................................................................................
    }

这是所有需要它的人的日志行为:

  

06-23 11:44:30.385: D / ActivityThread(27794):setTargetHeapUtilization:0.25   06-23 11:44:30.385: D / ActivityThread(27794):setTargetHeapIdealFree:8388608   06-23 11:44:30.385: D / ActivityThread(27794):setTargetHeapConcurrentStart:2097152   06-23 11:44:30.535: W / dalvikvm(27794):threadid = 1:线程退出,未捕获异常(group = 0x40ee7438)   06-23 11:44:30.535: E / AndroidRuntime(27794):致命异常:主要   06-23 11:44:30.535: E / AndroidRuntime(27794):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.myapp5 / com.example.myapp5.MainActivity}: java.lang.IllegalStateException:已为此实例调用setRenderer。   06-23 11:44:30.535: E / AndroidRuntime(27794):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)   06-23 11:44:30.535: E / AndroidRuntime(27794):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)   06-23 11:44:30.535: E / AndroidRuntime(27794):在android.app.ActivityThread.access $ 700(ActivityThread.java:143)   06-23 11:44:30.535: E / AndroidRuntime(27794):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1241)   06-23 11:44:30.535: E / AndroidRuntime(27794):在android.os.Handler.dispatchMessage(Handler.java:99)   06-23 11:44:30.535: E / AndroidRuntime(27794):在android.os.Looper.loop(Looper.java:137)   06-23 11:44:30.535: E / AndroidRuntime(27794):在android.app.ActivityThread.main(ActivityThread.java:4953)   06-23 11:44:30.535: E / AndroidRuntime(27794):at java.lang.reflect.Method.invokeNative(Native Method)   06-23 11:44:30.535: E / AndroidRuntime(27794):at java.lang.reflect.Method.invoke(Method.java:511)   06-23 11:44:30.535: E / AndroidRuntime(27794):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1004)   06-23 11:44:30.535: E / AndroidRuntime(27794):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)   06-23 11:44:30.535: E / AndroidRuntime(27794):at dalvik.system.NativeStart.main(Native Method)   06-23 11:44:30.535: E / AndroidRuntime(27794):引起:java.lang.IllegalStateException:已为此实例调用setRenderer。   06-23 11:44:30.535: E / AndroidRuntime(27794):在android.opengl.GLSurfaceView.checkRenderThreadState(GLSurfaceView.java:1810)   06-23 11:44:30.535: E / AndroidRuntime(27794):在android.opengl.GLSurfaceView.setEGLContextClientVersion(GLSurfaceView.java:472)   06-23 11:44:30.535: E / AndroidRuntime(27794):at com.example.myapp5.MainActivity $ MyGLSurfaceView。(MainActivity.java:80)   06-23 11:44:30.535: E / AndroidRuntime(27794):at com.example.myapp5.MainActivity.onCreate(MainActivity.java:31)   06-23 11:44:30.535: E / AndroidRuntime(27794):在android.app.Activity.performCreate(Activity.java:5160)   06-23 11:44:30.535: E / AndroidRuntime(27794):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)   06-23 11:44:30.535: E / AndroidRuntime(27794):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)   06-23 11:44:30.535: E / AndroidRuntime(27794):... 11更多

1 个答案:

答案 0 :(得分:1)

需要在setEGLContextClientVersion()之前调用

setRenderer(),如文档http://developer.android.com/reference/android/opengl/GLSurfaceView.html#setEGLContextClientVersion(int)中所述。

还有一个暗示,这是您复制的堆栈跟踪中的问题:

  

java.lang.IllegalStateException:已为此实例调用setRenderer。