当前位置: 代码迷 >> Android >> Handler的有关问题
  详细解决方案

Handler的有关问题

热度:145   发布时间:2016-05-01 22:23:09.0
Handler的问题
我自己写了个GameView,然后在Avtivity里面setContentView(gameView),在GameView那边的run方法中将游戏结束信息传递给Activity,然后Activity用setContentView(R.···)换布局,我试过了,可以把消息传过来,但是执行setContentView(R.···)会报空引用的错误,现在就是要当GameView那边游戏结束时候通知Avtivity更换布局,通知传递过来了,但是处理就出错了,请大牛们帮忙看下,

Activity里面的代码如下
Java code
protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  gameView=new GameView(this);  setContentView(gameView);  gameOver.post(gameView.MyThread);}public Handler gameOver = new Handler() {  @Override  public void handleMessage(Message msg) {   switch (msg.what) {   case 1:    System.out.println("gameover```````````````````");//这句话可以打印    setContentView(R.layout.choose_layout);//就是这里出错了    break;   default:    break;   }  }};GameView代码如下:Runnable MyThread =new Runnable (){  public void run() {   while (flag) {    canvas = holder.lockCanvas();                ·                ·                ·                ·    holder.unlockCanvasAndPost(canvas);    //GameOver处理    if (endGame()){     Message msg = gameViewActivity.gameOver.obtainMessage();     msg.arg1 = GAME_OVER;//GAME_OVER=1     gameViewActivity.gameOver.sendMessage(msg);    }   }  }};


------解决方案--------------------
project->clearn

refresh(F5)

试试
  相关解决方案