我试图在我的GLSurefaceView.Renderer类中设置我的初始游戏状态,并在InitialGameState(Context)中创建我的Sprite对象并在render()中调用它的方法,但在GLThread中抛出异常。
public class MyRenderer implements GLSurfaceView.Renderer
{
private Context m_context;
private GameStateManager m_game;
public MyRenderer(Context context)
{
m_context = context;
m_game = GameStateManager.getInstance();
m_game.setGameState(new InGame(m_context));
}
@Override
public void onDrawFrame(GL10 gl)
{
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
m_game.getCurrentState().render();
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height)
{
GLES20.glViewport(0, 0, width, height);
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config)
{
GLES20.glClearColor(0.63671875f, 0.76953125f, 0.22265625f, 1.0f);
ShaderManager.getInstance().createDefaultShaders(m_context);
}
}
public class InGame extends GameState
{
private Sprite m_sprite;
private boolean m_isInitialized = false;
public InGame(Context context)
{
super(context);
m_sprite = new Sprite(m_context);
}
@Override
public void render()
{
if (!m_isInitialized)
{
m_sprite.create("clover.png");
m_isInitialized = true;
}
else
{
m_sprite.begin();
m_sprite.draw();
m_sprite.end();
}
}
}
当我在onSurfaceCreated()中调用m_game.setGameState(new InGame(context))时,一切正常。可以修复吗?
Thread [<9> GLThread 10] (Suspended (exception NullPointerException))
<VM does not provide monitor information>
GLSurfaceView$GLThread.run() line: 1122