我已经阅读了有关关闭当前活动并通过活动堆栈等返回主活动的信息,但不幸的是,我一直收到 NullPointer,因为我需要关闭我的班级 Game 正在创建的子活动。
这是“游戏”类:
package pap.crowslanding;
public class Game extends MainActivity implements OnClickListener {
private MazeBall ball;
protected static GameView gameV;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tester1);
Button settings = (Button) findViewById(R.id.settingsButton);
Button mainMenu = (Button) findViewById(R.id.backButton);
ball = (MazeBall) findViewById(R.id.mazeball);
gameV = (GameView) findViewById(R.id.game_view);
settings.setOnClickListener(this);
mainMenu.setOnClickListener(this);
//Unrequired code removed
// IF USER PRESSES ON !Main Menu!
public void onClick(View v) {
switch (v.getId()){
case R.id.settingsButton:
break;
case R.id.backButton:
onBackPressed();
break;
}
}
public void onBackPressed() {
this.finish();//try activityname.finish instead of this
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
我的布局,R.Layout.tester1
是一个自定义布局,它运行一个 GameView 和 MazeBall 类,你可以在onCreate
方法中看到,我的 NullPointer 是指试图访问它的onDraw()
方法的 GameView,因为当我想去我的 MainActivity 时它仍在运行。
长话短说,无论如何要终止这些进程?我不能在标准意义上使用 finish(),因为 GameView 不扩展 Activity。