当前位置: 首页 > 知识库问答 >
问题:

使用float使我的应用在Android中崩溃

阎懿轩
2023-03-14
DisplayMetrics displayMetrics = new DisplayMetrics();
    ((Activity) getContext()).getWindowManager()
            .getDefaultDisplay()
            .getMetrics(displayMetrics);
final int height = displayMetrics.heightPixels;
final int width = displayMetrics.widthPixels;
public class OpenGL extends Fragment {
    private GLSurfaceView mGLView;
    private final float TOUCH_SCALE_FACTOR = 180.0f / 320;
    private float mPreviousX;
    private float mPreviousY;
    OpenGLRenderer mRenderer;

    public OpenGL() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        mGLView = new GLSurfaceView(getActivity());
        mGLView.setEGLContextClientVersion(2);
        mGLView.setRenderer(new OpenGLRenderer());
        // Render the view only when there is a change in the drawing data
        mGLView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);

        DisplayMetrics displayMetrics = new DisplayMetrics();
        ((Activity) getContext()).getWindowManager()
                .getDefaultDisplay()
                .getMetrics(displayMetrics);
        final int height = displayMetrics.heightPixels;
        final int width = displayMetrics.widthPixels;

        mGLView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent e) {

                // MotionEvent reports input details from the touch screen
                // and other input controls. In this case, you are only
                // interested in events where the touch position changed.

                float x = e.getX();
                float y = e.getY();

                switch (e.getAction()) {
                    case MotionEvent.ACTION_MOVE:
                        float dx = x - mPreviousX;
                        float dy = y - mPreviousY;

                        // reverse direction of rotation above the mid-line
                        if (y > height / 2) {
                            dx = dx * -1;
                        }

                        // reverse direction of rotation to left of the mid-line
                        if (x < width / 2) {
                            dy = dy * -1;
                        }

                        mRenderer.setAngle(
                                mRenderer.getAngle() +
                                        ((dx + dy) * TOUCH_SCALE_FACTOR));
                        mGLView.requestRender();
                }
                mPreviousX = x;
                mPreviousY = y;
                return true;
            }
        });
        return mGLView;
    }

    @Override
    public void onResume() {
        super.onResume();
        mGLView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mGLView.onPause();
    }
}

OpenGlenderer类

class OpenGLRenderer implements GLSurfaceView.Renderer {
    private Triangle mRectangle;
    // mMVPMatrix is an abbreviation for "Model View Projection Matrix"
    private final float[] mMVPMatrix = new float[16];
    private final float[] mProjectionMatrix = new float[16];
    private final float[] mViewMatrix = new float[16];
    private float[] mRotationMatrix = new float[16];
    float[] scratch = new float[16];
    public volatile float mAngle;

    public float getAngle() {
        return mAngle;
    }

    public void setAngle(float angle) {
        mAngle = angle;
    }

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        // Set the background frame color
        //GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

        mRectangle = new Triangle();
    }

    public void onDrawFrame(GL10 gl) {
        // Redraw background color
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);

        // Set the camera position (View matrix)
        Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);

        // Calculate the projection and view transformation
        Matrix.multiplyMM(mMVPMatrix, 0, mRotationMatrix, 0, mViewMatrix, 0);

        // Create a rotation for the triangle
        // long time = SystemClock.uptimeMillis() % 4000L;
        // float angle = 0.090f * ((int) time);
        Matrix.setRotateM(mRotationMatrix, 0, mAngle, 0, 0, -1.0f);

        // Combine the rotation matrix with the projection and camera view
        // Note that the mMVPMatrix factor *must be first* in order
        // for the matrix multiplication product to be correct.
        Matrix.multiplyMM(scratch, 0, mMVPMatrix, 0, mRotationMatrix, 0);

        // Draw triangle
        mRectangle.drawMatrix(scratch);
    }

    public void onSurfaceChanged(GL10 gl, int width, int height) {
        GLES20.glViewport(0, 0, width, height);
    }

    public static int loadShader(int type, String shaderCode){

        // create a vertex shader type (GLES20.GL_VERTEX_SHADER)
        // or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)
        int shader = GLES20.glCreateShader(type);

        // add the source code to the shader and compile it
        GLES20.glShaderSource(shader, shaderCode);
        GLES20.glCompileShader(shader);

        return shader;
    }
}

三角类

class Triangle {
    private final String vertexShaderCodeMatrix =
     // This matrix member variable provides a hook to manipulate
     // the coordinates of the objects that use this vertex shader
     "uniform mat4 uMVPMatrix;" +
             "attribute vec4 vPosition;" +
             "void main() {" +
             // the matrix must be included as a modifier of gl_Position
             // Note that the uMVPMatrix factor *must be first* in order
             // for the matrix multiplication product to be correct.
             "  gl_Position = uMVPMatrix * vPosition;" +
             "}";

    // Use to access and set the view transformation
    private int mMVPMatrixHandle;


    private final String vertexShaderCode =
         "attribute vec4 vPosition;" +
                 "void main() {" +
                 "  gl_Position = vPosition;" +
                 "}";

    private final String fragmentShaderCode =
         "precision mediump float;" +
                 "uniform vec4 vColor;" +
                 "void main() {" +
                 "  gl_FragColor = vColor;" +
                 "}";

    // number of coordinates per vertex in this array
    static final int COORDS_PER_VERTEX = 3;
    static float triangleCoords[] = {   // in counterclockwise order:
         0.0f,  0.622008459f, 0.0f, // top
         -0.5f, -0.311004243f, 0.0f, // bottom left
         0.5f, -0.311004243f, 0.0f  // bottom right
    };

    // Set color with red, green, blue and alpha (opacity) values
    float color[] = { 0.5f, 0.5f, 0.5f, 1.0f };

    private final int mProgram;

    private short[] indices = {0,1,2,0,2,3};

    private FloatBuffer vertexBuffer;
    private ShortBuffer indexBuffer;

    public Triangle() {

         // initialize vertex byte buffer for shape coordinates
         ByteBuffer bb = ByteBuffer.allocateDirect(
                 // (number of coordinate values * 4 bytes per float)
                 triangleCoords.length * 4);
         // use the device hardware's native byte order
         bb.order(ByteOrder.nativeOrder());

         // create a floating point buffer from the ByteBuffer
         vertexBuffer = bb.asFloatBuffer();
         // add the coordinates to the FloatBuffer
         vertexBuffer.put(triangleCoords);
         // set the buffer to read the first coordinate
         vertexBuffer.position(0);

         int vertexShader = OpenGLRenderer.loadShader(GLES20.GL_VERTEX_SHADER,
                 vertexShaderCode);
         int fragmentShader = OpenGLRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER,
                 fragmentShaderCode);

         // create empty OpenGL ES Program
         mProgram = GLES20.glCreateProgram();

         // add the vertex shader to program
         GLES20.glAttachShader(mProgram, vertexShader);

         // add the fragment shader to program
         GLES20.glAttachShader(mProgram, fragmentShader);

         // creates OpenGL ES program executables
         GLES20.glLinkProgram(mProgram);
    }

    private int mPositionHandle;
    private int mColorHandle;

    private final int vertexCount = triangleCoords.length / COORDS_PER_VERTEX;
    private final int vertexStride = COORDS_PER_VERTEX * 4; // 4 bytes per vertex

    public void draw() {
        // Add program to OpenGL ES environment
        GLES20.glUseProgram(mProgram);

        // get handle to vertex shader's vPosition member
        mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");

        // Enable a handle to the triangle vertices
        GLES20.glEnableVertexAttribArray(mPositionHandle);

        // Prepare the triangle coordinate data
        GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX,
             GLES20.GL_FLOAT, false,
             vertexStride, vertexBuffer);

        // get handle to fragment shader's vColor member
        mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");

        // Set color for drawing the triangle
        GLES20.glUniform4fv(mColorHandle, 1, color, 0);

        // Draw the triangle
        GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);

        // Disable vertex array
        GLES20.glDisableVertexAttribArray(mPositionHandle);
    }

    public void drawMatrix(float[] mvpMatrix) { // pass in the calculated transformation matrix


         // Add program to OpenGL ES environment
         GLES20.glUseProgram(mProgram);

         // get handle to vertex shader's vPosition member
         mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");

         // Enable a handle to the triangle vertices
         GLES20.glEnableVertexAttribArray(mPositionHandle);

         // Prepare the triangle coordinate data
         GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX,
                 GLES20.GL_FLOAT, false,
                 vertexStride, vertexBuffer);

         // get handle to fragment shader's vColor member
         mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");

         // Set color for drawing the triangle
         GLES20.glUniform4fv(mColorHandle, 1, color, 0);

         // Draw the triangle
         GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);

         // Disable vertex array
         GLES20.glDisableVertexAttribArray(mPositionHandle);

         // get handle to shape's transformation matrix
         mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");

         // Pass the projection and view transformation to the shader
         GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);

         // Draw the triangle
         GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);

         // Disable vertex array
         GLES20.glDisableVertexAttribArray(mPositionHandle);
    }
}

共有1个答案

邢凌
2023-03-14

您从不初始化mrenderer...变量

OpenGLRenderer mRenderer;

这就是mrenderer.getAngle()给出空指针异常的原因。

java.lang.NullPointerException:尝试对空对象引用调用虚拟方法“float com.example.tiagosilva.amob_android.opengrenderer.getAngle()”

mRenderer = new OpenGLRenderer();
 类似资料:
  • 更新到QRCodeReaderView最新版本后,我的应用程序崩溃仍然没有停止,不同的问题得到。致命异常:java.lang.RuntimeException:无法连接到java:580)在android.view.choreographer.doFrame(choreographer.java:550)在android.view.choreographer$FrameDisplayEventRe

  • 这就是它崩溃的地方 01-12 13:44:12.571 296 26-29688/ca.dti.grounded.app E/OtherService:AsynchChecker 287:73 java.lang.IllegalStateException:Parcel已完成!在android.os.binderproxy.transactNative(本机方法)在android.os.bind

  • 我正在尝试将图像添加到我新创建的应用程序中。但是,当我运行我的应用程序时,它会崩溃。我没有在我的应用程序中编写任何代码,因此我提供了XML代码和错误日志。我在以前的应用程序中使用了ImageViews。没有错误。我已经优化了我的图像,但它仍然给我错误。 图像详细信息 错误日志: 09-08 16:03:02.266 15137-15137/ooper.loopE/AndroidRuntime: F

  • 在我的应用程序中,我想在导航抽屉的片段中使用一个gridView,但我的应用程序正在崩溃。这是我的代码:

  • 我正在Eclipse中创建一个应用程序,现在我想在我的项目中使用appcompat库。我已经成功地导入了这个库,当我用AppCompatActivity扩展一些活动时,它工作得很好。问题是,当我运行应用程序时,我的应用程序启动由AppCompActivty扩展的活动。应用程序崩溃。Log Cat正在显示NoClassDefFoundError:请为我指引正确的方向。谢谢

  • 问题内容: 我试图弄乱新的RecyclerView,每当我尝试运行它时,我的应用程序立即崩溃。它为我提供了NullPointerException,用于尝试从中访问方法。我查看了其他帖子,发现大多数人都没有,但是我尝试了一下,但完全没有帮助。目前还不确定要怎么做,我们将不胜感激。这里是错误日志:(我会发布图片,但我还没有10位代表) 问题答案: 当没有提供时,通常会发生此问题。您可以这样做: