我已經閱讀了有關關閉當前活動並返回到主活動通過活動棧等,但不幸的是我不斷收到一個空指針,因爲我需要關閉的子活動,我類遊戲正在創建。按鈕返回到MainActivity和結束所有正在運行的孩子活動
這裏是類「遊戲」:
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
方法中看到的自定義佈局,我的空指針指GameView嘗試,因爲它仍在運行,當我想要去我的MainActivity訪問其onDraw()
方法。
長話短說,無論如何要終止這些進程?我無法使用標準意義上的finish(),因爲GameView不會擴展Activity。
就不行,甚至' – 2013-04-20 11:35:39