Android程序在启动时崩溃

时间:2015-04-20 19:26:50

标签: java android opengl-es

我一直在关注Android的Open GL上的this系列,但每当我运行第一个程序时它就会在启动时崩溃。我可能在他们的代码中遗漏了一些东西,但我检查了三次。它与活动xml文件有关吗?

这是完整的日志信息

04-20 15:53:13.695: D/OpenGLRenderer(2229): Use EGL_SWAP_BEHAVIOR_PRESERVED: true
04-20 15:53:13.698: D/(2229): HostConnection::get() New Host Connection established 0xb42b3e10, tid 2229
04-20 15:53:13.791: D/Atlas(2229): Validating map...
04-20 15:53:13.844: D/(2229): HostConnection::get() New Host Connection established 0xb42b3eb0, tid 2245
04-20 15:53:13.955: I/OpenGLRenderer(2229): Initialized EGL, version 1.4
04-20 15:53:14.002: D/OpenGLRenderer(2229): Enabling debug mode 0
04-20 15:53:14.025: W/EGL_emulation(2229): eglSurfaceAttrib not implemented
04-20 15:53:14.025: W/OpenGLRenderer(2229): Failed to set EGL_SWAP_BEHAVIOR on surface 0xb431f980, error=EGL_SUCCESS
04-20 15:53:14.119: D/(2229): HostConnection::get() New Host Connection established 0xb42b3ad0, tid 2244
04-20 15:53:14.214: E/AndroidRuntime(2229): FATAL EXCEPTION: GLThread 157
04-20 15:53:14.214: E/AndroidRuntime(2229): Process: com.test, PID: 2229
04-20 15:53:14.214: E/AndroidRuntime(2229): java.lang.IllegalArgumentException: No config chosen
04-20 15:53:14.214: E/AndroidRuntime(2229):     at android.opengl.GLSurfaceView$BaseConfigChooser.chooseConfig(GLSurfaceView.java:869)
04-20 15:53:14.214: E/AndroidRuntime(2229):     at android.opengl.GLSurfaceView$EglHelper.start(GLSurfaceView.java:1023)
04-20 15:53:14.214: E/AndroidRuntime(2229):     at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1400)
04-20 15:53:14.214: E/AndroidRuntime(2229):     at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1239)

MyGLActivity.class

 package com.test;

    import android.support.v7.app.ActionBarActivity;
    import android.app.Activity;
    import android.os.Bundle;
    import android.opengl.*;
    import android.opengl.GLSurfaceView.Renderer;
    public class MyGLActivity extends Activity {

        private GLSurfaceView glView;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            glView = new GLSurfaceView(this);
            glView.setRenderer(new MyGlRenderer(this));
            this.setContentView(glView);
        }

        @Override
        public void onPause(){
            super.onPause();
            glView.onPause();
        }

        @Override
        public void onResume(){
            super.onResume();
            glView.onResume();
        }


    }

MyGlRenderer.class

package com.test;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLSurfaceView;
import android.opengl.GLU;
import android.content.Context;
public class MyGlRenderer implements GLSurfaceView.Renderer{
    Context context;

    public  MyGlRenderer(Context context){
        this.context = context;
    }

    @Override
    public void onSurfaceCreated(GL10 gl,EGLConfig config){
        gl.glClearColor(100.0f,50.0f,75.0f,1.0f);
        gl.glClearDepthf(1.0f);
        gl.glEnable(GL10.GL_DEPTH_TEST);
        gl.glDepthFunc(GL10.GL_LEQUAL);
        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
        gl.glShadeModel(GL10.GL_SMOOTH);
        gl.glDisable(GL10.GL_DITHER);

    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        if(height == 0){ height = 1;}
        float aspect = (float)width / height;


        gl.glViewport(0,0,width,height);

        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();

        GLU.gluPerspective(gl, 45, aspect, 0.1f,100.f);

        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();


    }

    @Override
    public void onDrawFrame(GL10 gl) {
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);


    }

}

1 个答案:

答案 0 :(得分:0)

尽管文档未指定为强制性文档,但有时您似乎必须在GLSurfaceView.setRenderer之前调用GLSurfaceView.setEGLConfigChooser

试试这个初始化代码:

public class MyGLActivity extends Activity {
    //...    
    private GLSurfaceView glView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        glView = new GLSurfaceView(this);
        glView.setEGLConfigChooser(8 , 8, 8, 8, 16, 0);
        glView.setRenderer(new MyGlRenderer(this));
        this.setContentView(glView);
    }
    //...
}