gpt4 book ai didi

空对象引用上的 android.opengl.GLSurfaceView$GLThread.surfaceCreated()'

转载 作者:太空宇宙 更新时间:2023-11-03 12:03:01 24 4
gpt4 key购买 nike

我正在尝试使用 GLSurface 来显示自定义 View 。

我的xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="net.letsense.matrixtest.MainActivity">


<android.opengl.GLSurfaceView
android:layout_width="300dp"
android:layout_height="300dp"
android:id="@+id/my_surface_view"
/>

</android.support.constraint.ConstraintLayout>

我的主要

package net.letsense.matrixtest;

import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;

/**
* Created by borislegovic on 03/11/2017.
*/

public class Main extends Activity {
private int mPositionHandle;
private int mColorHandle;


private GLSurfaceView mGLView;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Create a GLSurfaceView instance and set it
// as the ContentView for this Activity.
// mGLView = new MyGLSurfaceView(this);
// mGLView = findViewById(R.id.my_surface_view);
//mGLView.setEGLContextClientVersion(2);
// mGLView = new MyGLSurfaceView(this);
// setContentView(mGLView);
}

@Override
protected void onPause() {
// The following call pauses the rendering thread.
// If your OpenGL application is memory intensive,
// you should consider de-allocating objects that
// consume significant memory here.
super.onPause();
// mGLView.onPause();
}

@Override
protected void onResume() {
// The following call resumes a paused rendering thread.
// If you de-allocated graphic objects for onPause()
// this is a good place to re-allocate them.
super.onResume();
// mGLView.onResume();
}

}

奇怪的是我没有对 surfaceView 对象做任何事情,它仍然让我崩溃:

java.lang.NullPointerException:尝试在空对象引用上调用虚方法“void android.opengl.GLSurfaceView$GLThread.surfaceCreated()” 在 android.opengl.GLSurfaceView.surfaceCreated(GLSurfaceView.java:524)

最佳答案

你的GLSurfaceView没有初始化,你需要初始化它。

首先创建类MyGLRenderer:

class MyGLRenderer implements GLSurfaceView.Renderer {
public void onSurfaceCreated(GL10 gl, EGLConfig config)
{
gl.glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
}

public void onSurfaceChanged(GL10 gl, int w, int h) {
gl.glViewport(0, 0, w, h);
}

public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
}
}

然后将其添加到您的OnCreate 方法中:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mGLView = (GLSurfaceView) this.findViewById(R.id.my_surface_view);
mGLView.setRenderer(new MyGLRenderer());

}

您可以阅读有关 OpenGL ES 的更多信息 here .

关于空对象引用上的 android.opengl.GLSurfaceView$GLThread.surfaceCreated()',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47139348/

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