java - When I push the 'home' button, my app gets killed -
here's code:
public class play extends activity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(new mainsurfaceview(this)); } @override protected void onpause() { super.onpause(); } public class mainsurfaceview extends surfaceview implements surfaceholder.callback { private surfaceholder msurfaceholder; private drawingthread mthread; private bitmap background; public mainsurfaceview(context context) { super(context); msurfaceholder = getholder(); msurfaceholder.addcallback(this); mthread = new drawingthread(); background = bitmapfactory.decoderesource(getresources(), r.drawable.background); } @override public void surfacechanged(surfaceholder holder, int format, int width, int height) { } @override public void surfacecreated(surfaceholder holder) { mthread.keeprunning = true; mthread.start(); } @override public void surfacedestroyed(surfaceholder holder) { mthread.keeprunning = false; boolean retry = true; while (retry) { try { mthread.join(); retry = false; } catch (interruptedexception e) { } } } private class drawingthread extends thread { boolean keeprunning = true; @override public void run() { canvas c; while (keeprunning) { c = null; try { c = msurfaceholder.lockcanvas(); synchronized (msurfaceholder) { c.drawbitmap(background, 0, 0, null); } } { if (c != null) msurfaceholder.unlockcanvasandpost(c); } try { thread.sleep(1); } catch (interruptedexception e) { } } } } } }
error log:
06-08 02:42:54.992: w/dalvikvm(10721): threadid=12: thread exiting uncaught exception (group=0x4154bba8) 06-08 02:42:54.992: e/androidruntime(10721): fatal exception: thread-478 06-08 02:42:54.992: e/androidruntime(10721): process: com.yong.space, pid: 10721 06-08 02:42:54.992: e/androidruntime(10721): java.lang.nullpointerexception 06-08 02:42:54.992: e/androidruntime(10721): @ com.yong.space.play$mainsurfaceview$drawingthread.run(play.java:81)
my bet lockcanvas fails when don't own screen, , returns null. should put in null check, , either kill thread or pause when onpause call- otherwise you'll busy loop , burn cycles trying draw failing.
Comments
Post a Comment