我刚开始在 android 上使用 open GL,而且对整体编程还是很陌生。我收到标题中提到的错误(
07-07 14:58:37.776: E/AndroidRuntime(884): FATAL EXCEPTION: main
07-07 14:58:37.776: E/AndroidRuntime(884): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.imlearningnow/com.example.imlearningnow.GLRenderEx}: java.lang.ClassCastException: com.example.imlearningnow.GLRenderEx
) 当我尝试打开活动时。该程序旨在渲染一个三角形。这是代码+ logcat:
GLRenderEx:
package com.example.imlearningnow;
import java.util.Random;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLSurfaceView.Renderer;
public class GLRenderEx implements Renderer{
private GLTriangleEx tri;
public GLRenderEx(){
tri = new GLTriangleEx();
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig eglConfig) {
// TODO Auto-generated method stub
gl.glDisable(GL10.GL_DITHER);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
gl.glClearColor(.8f, 0f, .2f, 1f);
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);
tri.draw(gl);
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
// TODO Auto-generated method stub
}
}
GLTriangleEX:
package com.example.imlearningnow;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import javax.microedition.khronos.opengles.GL10;
public class GLTriangleEx {
private float vertices[] = {
0f, 1f, //p0
1f, -1f, //p1
-1f, -1f //p2
};
private FloatBuffer vertBuff;
private short[] pIndex = {0,1, 2};
private ShortBuffer pBuff;
public GLTriangleEx(){
ByteBuffer bBuff = ByteBuffer.allocateDirect(vertices.length * 4);
bBuff.order(ByteOrder.nativeOrder());
vertBuff = bBuff.asFloatBuffer();
vertBuff.put(vertices);
vertBuff.position(0);
ByteBuffer pbBuff = ByteBuffer.allocateDirect(pIndex.length *2);
pbBuff.order(ByteOrder.nativeOrder());
pBuff = pbBuff.asShortBuffer();
pBuff.put(pIndex);
pBuff.position(0);
}
public void draw(GL10 gl){
gl.glFrontFace(GL10.GL_CW);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertBuff);
gl.glDrawElements(GL10.GL_TRIANGLES, pIndex.length, GL10.GL_UNSIGNED_SHORT, pBuff);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}
}
日志猫:
07-07 14:58:37.776: E/AndroidRuntime(884): FATAL EXCEPTION: main
07-07 14:58:37.776: E/AndroidRuntime(884): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.imlearningnow/com.example.imlearningnow.GLRenderEx}: java.lang.ClassCastException: com.example.imlearningnow.GLRenderEx
07-07 14:58:37.776: E/AndroidRuntime(884): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
07-07 14:58:37.776: E/AndroidRuntime(884): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-07 14:58:37.776: E/AndroidRuntime(884): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-07 14:58:37.776: E/AndroidRuntime(884): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-07 14:58:37.776: E/AndroidRuntime(884): at android.os.Handler.dispatchMessage(Handler.java:99)
07-07 14:58:37.776: E/AndroidRuntime(884): at android.os.Looper.loop(Looper.java:123)
07-07 14:58:37.776: E/AndroidRuntime(884): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-07 14:58:37.776: E/AndroidRuntime(884): at java.lang.reflect.Method.invokeNative(Native Method)
07-07 14:58:37.776: E/AndroidRuntime(884): at java.lang.reflect.Method.invoke(Method.java:521)
07-07 14:58:37.776: E/AndroidRuntime(884): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-07 14:58:37.776: E/AndroidRuntime(884): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-07 14:58:37.776: E/AndroidRuntime(884): at dalvik.system.NativeStart.main(Native Method)
07-07 14:58:37.776: E/AndroidRuntime(884): Caused by: java.lang.ClassCastException: com.example.imlearningnow.GLRenderEx
07-07 14:58:37.776: E/AndroidRuntime(884): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
07-07 14:58:37.776: E/AndroidRuntime(884): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
07-07 14:58:37.776: E/AndroidRuntime(884): ... 11 more
07-07 14:58:45.207: I/Process(884): Sending signal. PID: 884 SIG: 9