gpt4 book ai didi

android - ExceptionInInitializerError 异常

转载 作者:行者123 更新时间:2023-11-29 18:18:23 25 4
gpt4 key购买 nike

我正在尝试初始化 GL11,因为我在引用一个有

的方法时遇到了麻烦
GL11 gl 

作为它的参数。我试图在我的渲染器类中初始化它,但它没有用,所以我认为它的初始化搞乱了渲染器并创建了一个新类来初始化。

import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.egl.EGLDisplay;
import javax.microedition.khronos.opengles.GL11;

import android.opengl.GLU;



public class Unproject {

public static float setx;
public static float sety;
public static float posx, posy, posz;

EGLConfig[] configs = new EGLConfig[1];
EGLConfig config = configs[0];
static EGLContext glContext;
public static GL11 gl = (GL11)glContext.getGL();
EGLDisplay dpy = ((EGL10) gl).eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);

public Unproject() {
glContext = ((EGL10) gl).eglCreateContext(dpy, config, EGL10.EGL_NO_CONTEXT, null);
}

public static void vector3 (GL11 gl){

int[] viewport = new int[4];
float[] modelview = new float[16];
float[] projection = new float[16];
float winx, winy, winz;
float[] newcoords = new float[3];

gl.glGetIntegerv(GL11.GL_VIEWPORT, viewport, 0);
gl.glGetFloatv(GL11.GL_MODELVIEW_MATRIX, modelview, 0);
gl.glGetFloatv(GL11.GL_PROJECTION_MATRIX, projection, 0);

winx = (float)setx;
winy = (float)viewport[3] - sety;
winz = 0;

GLU.gluUnProject(winx, winy, winz, modelview, 0, projection, 0, viewport, 0, newcoords, 0);
posx = (int)newcoords[1];
posy = (int)newcoords[2];
posz = (int)newcoords[3];


}
}

Vector3 是我遇到问题的方法。我把它从渲染器移到了这个类中。

这是我的渲染器:

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.opengles.GL11;


import android.opengl.GLU;
import android.opengl.GLSurfaceView.Renderer;
import android.view.MotionEvent;


public class GLSurfaceRenderer implements Renderer{


public float setx, sety;
private float posx, posy, posz;
private double speed;
private static float rotation;
private static float statrotation;

GL11 gl;



private static FlatColoredSquare square;
private static FlatColoredSquare statSquare;



public GLSurfaceRenderer () {

square = new FlatColoredSquare();
statSquare = new FlatColoredSquare();
rotation = (float) Math.floor(Math.random()*361);
speed = 0.1;

}
public synchronized void randomMethod(MotionEvent event){
if (event.getAction() == MotionEvent.ACTION_DOWN){
setx = event.getX();
sety = event.getY();
Unproject.vector3(gl);
}
}

@Override
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT |
GL10.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glScalef(10, 10, 0);
gl.glPushMatrix();
gl.glRotatef(rotation, 0, 0, 1);
gl.glTranslatef((float) speed/10, 0, 0);
square.draw(gl);
gl.glPopMatrix();

gl.glPushMatrix();
gl.glTranslatef(posx, posy, posz);
gl.glRotatef(statrotation,0,0,1);
statSquare.draw(gl);
gl.glPopMatrix();


statrotation++;
speed++;

}





@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
GLU.gluOrtho2D(gl, 0.0f, width, 0.0f, height);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();

}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glShadeModel(GL10.GL_SMOOTH);
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);

}



}

不幸的是这段代码抛出一个ExceptionInInitializerError

08-01 17:01:34.672: ERROR/AndroidRuntime(421): Uncaught handler: thread main exiting due to uncaught exception
08-01 17:01:34.762: ERROR/AndroidRuntime(421): java.lang.ExceptionInInitializerError
08-01 17:01:34.762: ERROR/AndroidRuntime(421): at android.app.ui.GLSurfaceRenderer.randomMethod(GLSurfaceRenderer.java:44)
08-01 17:01:34.762: ERROR/AndroidRuntime(421): at android.app.ui.Practice.onTouchEvent(Practice.java:37)
08-01 17:01:34.762: ERROR/AndroidRuntime(421): at android.app.Activity.dispatchTouchEvent(Activity.java:2064)
08-01 17:01:34.762: ERROR/AndroidRuntime(421): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
08-01 17:01:34.762: ERROR/AndroidRuntime(421): at android.view.ViewRoot.handleMessage(ViewRoot.java:1690)
08-01 17:01:34.762: ERROR/AndroidRuntime(421): at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 17:01:34.762: ERROR/AndroidRuntime(421): at android.os.Looper.loop(Looper.java:123)
08-01 17:01:34.762: ERROR/AndroidRuntime(421): at android.app.ActivityThread.main(ActivityThread.java:4310)
08-01 17:01:34.762: ERROR/AndroidRuntime(421): at java.lang.reflect.Method.invokeNative(Native Method)
08-01 17:01:34.762: ERROR/AndroidRuntime(421): at java.lang.reflect.Method.invoke(Method.java:521)
08-01 17:01:34.762: ERROR/AndroidRuntime(421): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-01 17:01:34.762: ERROR/AndroidRuntime(421): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-01 17:01:34.762: ERROR/AndroidRuntime(421): at dalvik.system.NativeStart.main(Native Method)
08-01 17:01:34.762: ERROR/AndroidRuntime(421): Caused by: java.lang.NullPointerException
08-01 17:01:34.762: ERROR/AndroidRuntime(421): at android.app.ui.Unproject.<clinit>(Unproject.java:22)
08-01 17:01:34.762: ERROR/AndroidRuntime(421): ... 13 more

我能做些什么来纠正这个问题吗?我完全走错树了吗?我认为这可能是我在类之间共享变量的方式,是否有更好的方式来做到这一点?

最佳答案

Based on this documentation抛出 ExceptionInInitializerError 以指示在评估静态初始化程序或静态变量的初始化程序期间发生异常。检查您的代码是否有任何静态初始化逻辑。

关于android - ExceptionInInitializerError 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6901963/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com