简体   繁体   English

的ExceptionInInitializerError

[英]ExceptionInInitializerError

I'm trying to initialize GL11 because i was having troubling referencing a method that had 我正在尝试初始化GL11,因为我在麻烦地引用了具有

GL11 gl 

as its argument. 作为其论点。 I tried to initialize it in my renderer class but it didn't work so I figured that it the initialization was messing with the renderer and created a new class to initialize in. 我尝试在渲染器类中对其进行初始化,但无法正常工作,因此我发现初始化与渲染器发生了混乱,并创建了一个新类进行初始化。

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 is the method I was having trouble with. Vector3是我遇到麻烦的方法。 I moved it here into this class from the renderer. 我将其从渲染器移到了该类中。

Here is my renderer: 这是我的渲染器:

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);

}



}

Unfortuanatly this code throws an ExceptionInInitializerError 不幸的是,此代码引发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

Is there anything I can do to correct this? 有什么我可以纠正的吗? Am I completely up the wrong tree? 我是完全错在树上吗? I think it might be the way that I'm sharing variables between classes, is there a better way to do that? 我认为这可能是我在类之间共享变量的方式,有没有更好的方法呢?

Based on this documentation An ExceptionInInitializerError is thrown to indicate that an exception occurred during evaluation of a static initializer or the initializer for a static variable. 根据此文档,将引发ExceptionInInitializerError,以指示在评估静态初始化程序或静态变量的初始化程序期间发生了异常。 Check your code has any static initialization logic. 检查您的代码是否具有任何静态初始化逻辑。

It appears that the error you are getting is because of a NullPointerException in your class initialization. 看来您得到的错误是由于类初始化中的NullPointerException所致。

static EGLContext glContext; 
public static GL11 gl = (GL11)glContext.getGL();

You will notice you are attempting to call getGl() from an instance of EGLContext which isn't initialized. 您会注意到,您正在尝试从未初始化的EGLContext实例调用getGl()。 You will need to first assign glContext to something before you can use it. 您需要先将glContext分配给某些对象,然后才能使用它。

The stacktrace mentions <clinit> , which isn't very helpful if you don't know what it means. stacktrace提到了<clinit> ,如果您不知道它的含义,它不是很有用。 It is referencing class initialization, which is what happens when static members are initialized (as in this case), but it could also reference a static initialization block that looks like this: 它引用的是类初始化,这是在初始化静态成员时发生的情况(在这种情况下),但是它也可以引用如下所示的静态初始化块:

static {
    //some static init code
}

The reason that ExceptionInInitializerError is throw is likely because something higher up is catching all Exceptions, and wrapping them in the ExceptionInInitializerError . 抛出ExceptionInInitializerError的原因很可能是因为更高的位置正在捕获所有Exception,并将它们包装在ExceptionInInitializerError

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM