Im trying to use GLSurface to display a custom view.
My 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>
My Main
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();
}
}
The strange thing is i dont do anything with surfaceView object and it still gives me this crash :
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.opengl.GLSurfaceView$GLThread.surfaceCreated()' on a null object reference at android.opengl.GLSurfaceView.surfaceCreated(GLSurfaceView.java:524)
Your GLSurfaceView is not initialized, you need to initialize it.
First create class 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);
}
}
And then add this to your OnCreate method:
@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());
}
You can read more about OpenGL ES here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With