【问题标题】:Android openGL renderer returning null pointer exceptionAndroid openGL 渲染器返回空指针异常
【发布时间】:2013-03-01 13:12:36
【问题描述】:

我正在创建一个使用 GL10 的应用程序,它由三个类组成,

A 类扩展了 Activity。 B 类实现渲染器。 C 类扩展了 Activity。

C 类包含 3D 立方体的数据,B 类是渲染器,A 类显示它。

为了显示它,我在 A 类中使用以下方法,

GLCubeRenderer ourSurface = new GLCubeRenderer();

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);



    GLSurfaceView glSurfaceView =
        (GLSurfaceView) findViewById(R.id.ourCube);
    glSurfaceView.setRenderer(ourSurface);
    setContentView(R.layout.cubelayout);

}

XML 是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<com.mastermind.GLCubeRenderer
    android:id="@+id/ourCube"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />


</RelativeLayout>

问题在于,在 A 类的 Java 代码中,'glSurfaceView.setRenderer(ourSurface);' 行返回空指针异常。

B 类(渲染器)代码:

  private GLCube cube = new GLCube();
static Context context;
public static float xAngle;

public static float yAngle;

final float[] ambient = { 0.1f, 1, 1, 1 };
final float[] position = { 45, 20, 0, 1 };
final float[] direction = { 0, -1, 0 };

public GLCubeRenderer() {
    cube = new GLCube();



}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    // TODO Auto-generated method stub
    gl.glDisable(GL10.GL_DITHER);
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
    gl.glEnable(GL10.GL_LIGHT1);
    gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_DIFFUSE, ambient, 0);
    gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_POSITION, position, 0);
    gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_SPOT_DIRECTION, direction, 0);
    gl.glLightf(GL10.GL_LIGHT1, GL10.GL_SPOT_CUTOFF, 30.0f);
    gl.glClearColor(.0f, 0, .0f, 0);
    gl.glClearDepthf(1f);


}

@Override
public void onDrawFrame(GL10 gl) {
    // TODO Auto-generated method stub
    gl.glDisable(GL10.GL_DITHER);
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();
    GLU.gluLookAt(gl, 0, 0, -5, 0, 0, 0, 0, 2, 0);

    gl.glRotatef(xAngle, 0, xAngle, 0);
    // gl.glRotatef(yAngle, yAngle, 0, yAngle);

    gl.glActiveTexture(GL10.GL_TEXTURE0);
    gl.glTexEnvx(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE,     GL10.GL_MODULATE);
    gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);
    gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);

    cube.draw(gl);
}



@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
    // TODO Auto-generated method stub
    gl.glViewport(0, 0, width, height);
    float ratio = (float) width / height;
    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();
    gl.glFrustumf(-ratio, ratio, -1, 1f, 1, 25);

}

还有C类代码:

private float vertices[] = {
        1, 1, -1, // p0-top front right
        1, -1, -1, // p1-bottom front right
        -1, -1, -1, // p2-bottom front left
        -1, 1, -1, // p3-front top left
        1, 1, 1, // p4-top back right
        1, -1, 1, // p5-bottom back right
        -1, -1, 1, // p6-bottom back left
        -1, 1, 1 // p7-front back left

};

// Buffer for our vertices
private FloatBuffer vertBuff;

// Index for our points e.g. p0 = 0f, 1f, in vert Index
private short[] pIndex = { 
        3, 4, 0,
        0, 4, 1,
        3, 0, 1,
        3, 7, 4,
        7, 6, 4,
        7, 3, 6,
        3, 1, 2,
        1, 6, 2,
        6, 3, 2,
        1, 4, 5,
        5, 6, 1,
        6, 5, 4 
        };

// Buffer for points index
private ShortBuffer pBuff;

// Triangle constructor
public GLCube() {

    // Construction of vertices
    // byte buffer for vertices
    ByteBuffer bBuff = ByteBuffer.allocateDirect(vertices.length * 4);
    bBuff.order(ByteOrder.nativeOrder());
    vertBuff = bBuff.asFloatBuffer();
    vertBuff.put(vertices);
    vertBuff.position(0);

    // Construction of points
    // point byte buffer
    ByteBuffer pointByteBuff = ByteBuffer.allocateDirect(pIndex.length * 2);
    pointByteBuff.order(ByteOrder.nativeOrder());
    pBuff = pointByteBuff.asShortBuffer();
    pBuff.put(pIndex);
    pBuff.position(0);
}



public void draw(GL10 gl) {

    gl.glFrontFace(GL10.GL_CW);
    gl.glEnable(GL10.GL_CULL_FACE);
    gl.glCullFace(GL10.GL_BACK);
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertBuff);
    gl.glDrawElements(GL10.GL_TRIANGLES, pIndex.length,   GL10.GL_UNSIGNED_SHORT, pBuff);
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glDisable(GL10.GL_CULL_FACE);
}

请原谅问题的长度,但有人有任何解决方案吗?

【问题讨论】:

    标签: java android opengl-es nullpointerexception


    【解决方案1】:

    我不明白,你在你的XML中定义ourCube为GLCubeRenderer,和ourSurface是同一个类。

    GLSurfaceView glSurfaceView =
        (GLSurfaceView) findViewById(R.id.ourCube); 
    

    <com.mastermind.GLCubeRenderer
         android:id="@+id/ourCube"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         />
    

    然后,当获取 ourCube 的引用时,您再次将其转换为 GLSurfaceView。

    GLCubeRenderer 是否扩展了 GLSurfaceView?在这种情况下,您是否尝试将此表面视图的 Renderer 设置为同一类的实例?

    我想你可能在这里混淆了课程。

    【讨论】:

    • 也许,我试图通过使用这个答案来解决这个问题:stackoverflow.com/questions/4120689/…
    • 将 xml 元素创建为 GLSurfaceView,让您的 glSurfaceView 扩展 Renderer,然后使用相同的实例。 GLSurfaceView glSurfaceView = (GLSurfaceView) findViewById(R.id.ourCube); glSurfaceView.setRenderer(glSurfaceView);
    猜你喜欢
    • 2014-07-03
    • 2014-08-25
    • 1970-01-01
    • 2015-06-24
    • 1970-01-01
    • 2014-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多