我无法弄清楚为什么我的新活动不会开始(我已经省略了所有不相关的代码)
我想在时间结束后显示主菜单
public class GRenderer implements Renderer
{
public void timer ()
{
Game.currentGameTime = System.currentTimeMillis();
Game.ellapsedTime = Game.currentGameTime - Game.gameStartTime;
if (Game.ellapsedTime > 10000)
{
// THIS IS WHEN The GAME SHOULD END
Toast.makeText(Game.context, "10Seconds", Toast.LENGTH_SHORT).show();
Intent MainMenu = new Intent(getApplicationContext(),MainMenu.class);
startActivity(MainMenu);
}
}
@Override
public void onDrawFrame(GL10 gl) // when drawing to screen
{
timer();
}
}
这是logcat:
12-13 18:28:28.500: E/AndroidRuntime(20184): FATAL EXCEPTION: GLThread 1386
12-13 18:28:28.500: E/AndroidRuntime(20184): Process: com.damienrenner.spacefruitshooter, PID: 20184
12-13 18:28:28.500: E/AndroidRuntime(20184): java.lang.NullPointerException
12-13 18:28:28.500: E/AndroidRuntime(20184): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
12-13 18:28:28.500: E/AndroidRuntime(20184): at com.damienrenner.spacefruitshooter.GameLoop.timer(GameLoop.java:280)
12-13 18:28:28.500: E/AndroidRuntime(20184): at com.damienrenner.spacefruitshooter.GameLoop.onDrawFrame(GameLoop.java:296)
12-13 18:28:28.500: E/AndroidRuntime(20184): at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1523)
12-13 18:28:28.500: E/AndroidRuntime(20184): at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)
答案 0 :(得分:0)
使用以下代码:
Intent MainMenu = new Intent(YourActivity.this,MainMenu.class);
startActivity(MainMenu);
而不是
Intent MainMenu = new Intent(getApplicationContext(),MainMenu.class);
startActivity(MainMenu);
有关getApplicationContext()的更多信息,请阅读this
答案 1 :(得分:0)
您只能从其他活动开始活动。看起来像GRenderer不是你的活动。你应该将它传递给你的GLSurfaceView活动。