我有一个类扩展名为 GameView 的视图。在 MainActivity 我把它作为 contentView:
if(Const.gameView == null){
Const.gameView = new GameView(this);
Const.gameView.setViews(Const.chickenArr,Const.chickenViewArr,message,score_message , this.importantMessage , this.showTimerMessage);
setContentView(Const.gameView);
}
在这里我面临一个问题。当我退出活动然后返回时,我想再次显示我的 GameView。
当我使用上面的代码时,当我再次回到 MainActivity 时,我没有看到我的 gameView。当我通过设置 setContentView(Const.gameView); 更改代码时 在“如果”之外我得到一个错误
11-10 22:17:35.821: E/AndroidRuntime(1580): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
我该怎么办?
游戏视图:
public GameView(Context context) {
super(context);
int picture = Const.a1;
if(backgroundBitmap == null)
backgroundBitmap = BitmapFactory.decodeResource(getResources(), picture);
// TODO Auto-generated constructor stub
}
public void setViews(Chicken[] chickenArr, ChickenView[] chickenViewArr,Messages message , Messages messageScore,
Messages gameoverMes , Messages showRemailTimeMes) {
this.chickenArr = chickenArr;
this.chickenViewArr = chickenViewArr;
this.message=message;
this. messageScore = messageScore;
this.gameoverMes =gameoverMes;
this.showRemailTimeMes=showRemailTimeMes;
}
@Override
public void onDraw(Canvas canvas)
{
canvas.drawBitmap(this.backgroundBitmap, 1, 1, null);
//meassage
this.message.onDraw(canvas);
......
}