Java Android openGL渲染器返回空指针异常
我正在创建一个应用程序,使用GL10,它由三个类组成 A类扩展活动。 类B实现渲染器。 C类扩展活动 类C包含3D立方体的数据,类B是渲染器,类a显示它 要显示它,我在类A中使用以下方法Java Android openGL渲染器返回空指针异常,java,android,opengl-es,nullpointerexception,Java,Android,Opengl Es,Nullpointerexception,我正在创建一个应用程序,使用GL10,它由三个类组成 A类扩展活动。 类B实现渲染器。 C类扩展活动 类C包含3D立方体的数据,类B是渲染器,类a显示它 要显示它,我在类A中使用以下方法 GLCubeRenderer ourSurface = new GLCubeRenderer(); public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); GLSurface
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>
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);
}
请原谅问题的长度,但有人有任何解决方案吗?我不明白,您在XML中将我们的多维数据集定义为glcubernderer,它与我们的Surface是同一个类
GLSurfaceView glSurfaceView =
(GLSurfaceView) findViewById(R.id.ourCube);
及
然后,在获取我们的多维数据集的引用时,再次将其转换为GLSurfaceView
GLCubeRenderer是否扩展了GLSURFACHEVIEW?在这种情况下,是否尝试将此surfaceview的渲染器设置为同一类的实例
我想您可能在这里混淆了类。也许,我正试图通过使用以下答案来解决这个问题:将xml元素创建为GLSURFACHEVIEW,使您的GLSURFACHEVIEW扩展呈现程序,然后使用相同的实例。GLSurfaceView GLSurfaceView=(GLSurfaceView)findViewById(R.id.ourCube);glSurfaceView.setRenderer(glSurfaceView);
GLSurfaceView glSurfaceView =
(GLSurfaceView) findViewById(R.id.ourCube);
<com.mastermind.GLCubeRenderer
android:id="@+id/ourCube"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>