i developing game has 2 activities camera surfaceview , fire button in addition weapon imageview.the 2 activities have same code.after switching between activities several times app crashes. code , below stack trace:
public class mainactivity extends activity implements surfaceholder.callback,onclicklistener { camera mcamera; surfaceview mpreview; button shoot; textview rounds; int shootsound; int reloadsound; int music; soundpool spool; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); new tapcontextsdk(getapplicationcontext()).initialize(); new tapcontextsdk(getapplicationcontext()).showad(); mpreview = (surfaceview)findviewbyid(r.id.preview); mpreview.getholder().addcallback(this); mpreview.getholder().settype(surfaceholder.surface_type_push_buffers); mcamera = camera.open(); shoot=(button)findviewbyid(r.id.button1); shoot.setonclicklistener(this); rounds=(textview)findviewbyid(r.id.textview1); this.setvolumecontrolstream(audiomanager.stream_music); spool = new soundpool(1, audiomanager.stream_music, 0); shootsound= spool.load(this, r.raw.shoot, 2); reloadsound= spool.load(this, r.raw.reload, 2); music= spool.load(this, r.raw.music, 2); new tapcontextsdk(getapplicationcontext()).initialize(); new tapcontextsdk(getapplicationcontext()).showad(); } @override public void onpause() { super.onpause(); mcamera.stoppreview(); } @override public void ondestroy() { super.ondestroy(); mcamera.release(); } @override public void surfacechanged(surfaceholder holder, int format, int width, int height) { parameters parameters = mcamera.getparameters(); list<camera.size> previewsizes = parameters.getsupportedpreviewsizes(); camera.size previewsize = previewsizes.get(4); //480h x 720w parameters.setpreviewsize(previewsize.width, previewsize.height); parameters.setflashmode(parameters.flash_mode_auto); parameters.setfocusmode(camera.parameters.focus_mode_auto); mcamera.setparameters(parameters); display display = ((windowmanager)getsystemservice(context.window_service)).getdefaultdisplay(); if(display.getrotation() == surface.rotation_0) { mcamera.setdisplayorientation(90); } else if(display.getrotation() == surface.rotation_270) { mcamera.setdisplayorientation(180); } mcamera.startpreview(); } @override public void surfacecreated(surfaceholder holder) { try { mcamera.setpreviewdisplay(mpreview.getholder()); } catch (exception e) { e.printstacktrace(); } } @override public void surfacedestroyed(surfaceholder holder) { log.i("preview","surfacedestroyed"); } public void shoot(){ audiomanager audiomanager = (audiomanager) getsystemservice(audio_service); float volume = (float) audiomanager.getstreamvolume(audiomanager.stream_music); spool.play(shootsound, volume, volume, 1, 0, 1f); } public void reload(){ audiomanager audiomanager = (audiomanager) getsystemservice(audio_service); float volume = (float) audiomanager.getstreamvolume(audiomanager.stream_music); spool.play(reloadsound, volume, volume, 1, 0, 1f); } public void music(){ audiomanager audiomanager = (audiomanager) getsystemservice(audio_service); float volume = (float) audiomanager.getstreamvolume(audiomanager.stream_music); spool.play(music, volume, volume, 1, 0, 1f); } public void vibrateme() { vibrator vibrate = (vibrator)getsystemservice(context.vibrator_service); vibrate.vibrate(100); } @override public void onclick(view arg0) { // todo auto-generated method stub switch(arg0.getid()) { case r.id.button1: int tmp = integer.valueof( rounds.gettext().tostring()); if(tmp==0) { rounds.settext("12"); reload(); } else { shoot(); vibrateme(); rounds.settext( string.valueof( tmp-1) ); } break; } }}
stack trace:
07-11 17:42:15.241: e/androidruntime(28651): fatal exception: main 07-11 17:42:15.241: e/androidruntime(28651): java.lang.runtimeexception: unable start activity componentinfo{com.jouni.camsniper/com.jouni.camsniper.machinegun}: java.lang.runtimeexception: fail connect camera service 07-11 17:42:15.241: e/androidruntime(28651): @ android.app.activitythread.performlaunchactivity(activitythread.java:2194) 07-11 17:42:15.241: e/androidruntime(28651): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2229) 07-11 17:42:15.241: e/androidruntime(28651): @ android.app.activitythread.access$600(activitythread.java:139) 07-11 17:42:15.241: e/androidruntime(28651): @ android.app.activitythread$h.handlemessage(activitythread.java:1261) 07-11 17:42:15.241: e/androidruntime(28651): @ android.os.handler.dispatchmessage(handler.java:99) 07-11 17:42:15.241: e/androidruntime(28651): @ android.os.looper.loop(looper.java:154) 07-11 17:42:15.241: e/androidruntime(28651): @ android.app.activitythread.main(activitythread.java:4945) 07-11 17:42:15.241: e/androidruntime(28651): @ java.lang.reflect.method.invokenative(native method) 07-11 17:42:15.241: e/androidruntime(28651): @ java.lang.reflect.method.invoke(method.java:511) 07-11 17:42:15.241: e/androidruntime(28651): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:784) 07-11 17:42:15.241: e/androidruntime(28651): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:551) 07-11 17:42:15.241: e/androidruntime(28651): @ dalvik.system.nativestart.main(native method) 07-11 17:42:15.241: e/androidruntime(28651): caused by: java.lang.runtimeexception: fail connect camera servicef 07-11 17:42:15.241: e/androidruntime(28651): @ android.hardware.camera.native_setup(native method) 07-11 17:42:15.241: e/androidruntime(28651): @ android.hardware.camera.<init>(camera.java:307) 07-11 17:42:15.241: e/androidruntime(28651): @ android.hardware.camera.open(camera.java:283) 07-11 17:42:15.241: e/androidruntime(28651): @ com.jouni.camsniper.machinegun.oncreate(machinegun.java:52) 07-11 17:42:15.241: e/androidruntime(28651): @ android.app.activity.performcreate(activity.java:4531) 07-11 17:42:15.241: e/androidruntime(28651): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1071) 07-11 17:42:15.241: e/androidruntime(28651): @ android.app.activitythread.performlaunchactivity(activitythread.java:2150) 07-11 17:42:15.241: e/androidruntime(28651): ... 11 more 07-11 17:42:18.924: d/process(28651): killprocess, pid=28651
any suggestion how make app more stable (no crashes) ??
Comments
Post a Comment