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. 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())
. 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.
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());
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.