简体   繁体   English

如果在触摸屏幕时打进电话会崩溃

[英]Crash if phone call comes in while touching screen

I have a game app which is mostly working fine. 我有一个游戏应用程序,基本上可以正常运行。 If a phone call comes in during the game (and I am not currently touching the screen) then the game copes with the subsequent onPause and onResume exactly as I would expect and it carriese on from where it left off. 如果在游戏中打来电话(并且我目前未触摸屏幕),则游戏将完全按照我的期望处理后续的onPause和onResume,并且从中断处继续进行。 However if I am touching the screen at the moment the call comes in the program crashes. 但是,如果我正在触摸屏幕,那么程序中的调用就会崩溃。 Logcat reports a NullPointerException at the line synchronized (micks_thread_thing.getSurfaceHolder()) . Logcat在synchronized (micks_thread_thing.getSurfaceHolder())synchronized (micks_thread_thing.getSurfaceHolder())报告NullPointerException I presume that sometimes the getSurfaceHolder can fail, but I'm not sure how I could have written the code differently to avoid the error. 我认为有时getSurfaceHolder可能会失败,但是我不确定如何以不同的方式编写代码以避免错误。

    public boolean onTouchEvent(MotionEvent event) 
    {
      super.onTouchEvent(event); // not sure if I need this

      synchronized (micks_thread_thing.getSurfaceHolder()) // this is the line causing the nullpointerexception
      {
        if (event.getAction() == MotionEvent.ACTION_DOWN) 
        {
          do_down(event.getX(),event.getY());
        }
        if (event.getAction() == MotionEvent.ACTION_MOVE) 
        {
          do_move(event.getX(),event.getY());
        }

        if (event.getAction() == MotionEvent.ACTION_UP) 
        {
          do_up(event.getX(),event.getY());
        }
      }
      return true;
    }

try wrapping your entire synchronized block in a null check on your thread: 尝试将整个同步块包装在线程的空检查中:

if(micks_thread_thing != null) 
{
synchronized (micks_thread_thing.getSurfaceHolder()) // this is the line causing the nullpointerexception
  {
    if (event.getAction() == MotionEvent.ACTION_DOWN) 
    {
      do_down(event.getX(),event.getY());
    }
    if (event.getAction() == MotionEvent.ACTION_MOVE) 
    {
      do_move(event.getX(),event.getY());
    }

    if (event.getAction() == MotionEvent.ACTION_UP) 
    {
      do_up(event.getX(),event.getY());
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM