如果我想使用SurfaceView上方的任何原生界面,则着色器编译失败。
如果我开始这样的应用程序一切正常:
surfaceView = new mSurfaceView(this);
renderer = new mRender();
surfaceView.setRenderer(renderer);
setContentView(surfaceView);
但如果我想使用任何接口,它就会失败。我正在应用这样的布局:
setContentView(R.layout.main);
surfaceView = (mSurfaceView) findViewById(R.id.gl_surface_view);
renderer = new mRender();
surfaceView.setRenderer(renderer);
应用程序在编译着色器时崩溃。这是我的代码:
protected int compileShader(int shaderType, String shaderCode)
{
int shaderHandle = GLES20.glCreateShader(shaderType);
if (shaderHandle != 0)
{
// Pass in the shader source.
GLES20.glShaderSource(shaderHandle, shaderCode);
// Compile the shader.
GLES20.glCompileShader(shaderHandle);
// Get the compilation status.
final int[] compileStatus = new int[1];
GLES20.glGetShaderiv(shaderHandle, GLES20.GL_COMPILE_STATUS, compileStatus, 0);
// If the compilation failed, delete the shader.
if (compileStatus[0] == 0)
{
GLES20.glDeleteShader(shaderHandle);
shaderHandle = 0;
}
}
if (shaderHandle == 0)
{
throw new RuntimeException("Error creating shader.");
}
return shaderHandle;
}
Shader未成功编译,因此shaderHandle为0且应用程序在此行崩溃:
throw new RuntimeException("Error creating shader.");
它没有布局编译好。我认为问题在于,在第一个变体中我用这种方式创建布局:surfaceView = new mSurfaceView(this);但是在崩溃变种中我用这种方式创建它:surfaceView =(mSurfaceView)findViewById(R.id.gl_surface_view);我认为因为它而崩溃,但我不知道为什么。它对我来说很好,我没有看到问题。
这是表面视图在main.xml中的外观:
<engine.core.mSurfaceView
android:id="@+id/gl_surface_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />