I am currently editing the AprilTags code from GitHub found here: https://github.com/johnjwang/apriltag-android/tree/master/src/main. I am trying to set a TextView in an inner static class.
static class LinesProgram {
private static final String vertexShaderCode =
"uniform mat4 uMVPMatrix;\n" +
"attribute vec4 aPosition;\n" +
"void main() {\n" +
" gl_Position = uMVPMatrix * aPosition;\n" +
"}\n";
private static final String fragmentShaderCode =
"precision mediump float;\n" +
"uniform vec4 uColor;\n" +
"void main() {\n" +
" gl_FragColor = uColor;\n" +
"}\n";
private FloatBuffer buffer;
private Context context;
int programId;
int aPositionLoc;
int uMVPMatrixLoc;
int uColorLoc;
public LinesProgram(Context context) {
// Compile the shader code into a GL program
this.context = context;
int vid = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
int fid = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);
programId = GLES20.glCreateProgram();
checkGlError("glCreateProgram");
GLES20.glAttachShader(programId, vid);
checkGlError("glAttachShader");
GLES20.glAttachShader(programId, fid);
checkGlError("glAttachShader");
GLES20.glLinkProgram(programId);
int[] linkStatus = new int[1];
GLES20.glGetProgramiv(programId, GLES20.GL_LINK_STATUS, linkStatus, 0);
if (linkStatus[0] != GLES20.GL_TRUE) {
Log.e(TAG, "Could not link program: ");
Log.e(TAG, GLES20.glGetProgramInfoLog(programId));
GLES20.glDeleteProgram(programId);
}
// Get handles to attributes and uniforms
aPositionLoc = GLES20.glGetAttribLocation(programId, "aPosition");
checkGlError("get aPosition");
uMVPMatrixLoc = GLES20.glGetUniformLocation(programId, "uMVPMatrix");
checkGlError("get uMVPMatrix");
uColorLoc = GLES20.glGetUniformLocation(programId, "uColor");
checkGlError("get uColor");
}
// points = [x0 y0 x1 y1 ...]
// npoints = number of xy pairs in points (e.g. points is expected to have
// a length of at least 2*npoints)
public void draw(float[] PVM, float[] points, int npoints, float[] rgba, int type) {
// Reuse points buffer if possible
if (buffer == null || 2*npoints > buffer.capacity()) {
int nbytes = 4 * 2*npoints;
ByteBuffer bb = ByteBuffer.allocateDirect(nbytes);
bb.order(ByteOrder.nativeOrder());
buffer = bb.asFloatBuffer();
}
buffer.position(0);
buffer.put(points, 0, 2*npoints);
buffer.position(0);
GLES20.glUseProgram(programId);
checkGlError("glUseProgram");
GLES20.glUniformMatrix4fv(uMVPMatrixLoc, 1, false, PVM, 0);
checkGlError("glUniformMatrix4fv");
GLES20.glUniform4fv(uColorLoc, 1, rgba, 0);
checkGlError("glUniform4fv");
// Render frame
GLES20.glEnableVertexAttribArray(aPositionLoc);
GLES20.glVertexAttribPointer(aPositionLoc, 2,
GLES20.GL_FLOAT, false, 8, buffer);
GLES20.glLineWidth(4.0f);
GLES20.glDrawArrays(type, 0, npoints);
GLES20.glDisableVertexAttribArray(aPositionLoc);
GLES20.glUseProgram(0);
TextView coordinates = (TextView) ((Activity)context).findViewById(R.id.textView1);
//for (int i = 0; i < points.length; i++) {
// coordinates.setText(String.valueOf(points[i]));
//}
}
}
However, I am getting this error:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.app.Activity.findViewById(int)' on a null object reference
and the app crashes when it gets to where I set the TextView.
I'm sure the text view id is the same as defined in the main.xml file. The TextView is within a FrameLayout if that is important.
In the onCreate function I defined at the top, public TextView coordinates;
.