java - NullPointerException in setPreviewDisplay(holder) -


pretty new on android stuff , gets nullpointexception can't seem figure out. trying implement onresume() method in cameraactivity , have moved of orginal code in oncreate() onresume() , call onresume() in oncreate(). activity worked fine when code in oncreate(), when placed in onresume() exception arsises. causing it?

package com.example.tensioncamapp_project;  import java.io.ioexception;  import android.content.context; import android.hardware.camera; import android.util.log; import android.view.surfaceholder; import android.view.surfaceview;  /** basic camera preview class */ public class camerapreview extends surfaceview implements surfaceholder.callback { private static final string tag = "previewaactivity"; private surfaceholder mholder; private camera mcamera;   public camerapreview(context context, camera camera) {     super(context);     this.mcamera = camera;      // install surfaceholder.callback notified when     // underlying surface created , destroyed.     this.mholder = getholder();     this.mholder.addcallback(this); }  /**displays picture on camera */ public void surfacecreated(surfaceholder holder) {     // surface has been created, tell camera draw preview.     try {         this.mcamera.setpreviewdisplay(holder);         this.mcamera.startpreview();     } catch (ioexception e) {         log.d(tag, "error setting camera preview: " + e.getmessage());     } }  public void surfacedestroyed(surfaceholder holder) {    this.mcamera.release();     this.mcamera = null; }  public void surfacechanged(surfaceholder holder, int format, int w, int h) {     //add things here } } 

and cameraactivityclass

package com.example.tensioncamapp_project;  import java.io.file; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import java.sql.date; import java.text.simpledateformat; import android.content.intent; import android.app.activity; import android.graphics.bitmap; import android.hardware.camera; import android.hardware.camera.parameters; import android.hardware.camera.picturecallback; import android.os.bundle; import android.os.environment; import android.util.log; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.framelayout; import android.widget.imagebutton; import android.widget.imageview;  public class cameraactivity extends activity { private imagebutton capturebutton; private camera mcamera;     private camerapreview mpreview;     private picturecallback mpicture;     private imageview imageview;     private static final int std_delay = 400;     private static final int media_type_image = 1; protected static final string tag = "cameraactivity";  /**starts camera */ @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_camera);     onresume();  }      /**connects capture button on view listener  *  , redirects client preview of captures image*/ private void addlisteneronbutton() {     this.capturebutton = (imagebutton) findviewbyid(r.id.button_capture_symbol);     this.capturebutton.setonclicklistener(new view.onclicklistener() {          @override         public void onclick(view capturebutton) {             mcamera.takepicture(null, null, mpicture);             delay();             intent viewpic = new intent(cameraactivity.this,      viewpicactivity.class);             startactivity(viewpic);         }     }); }   /** safe way instance of camera object. code collected elsewhere */ public static camera getcamerainstance(){     camera c = null;     try {         // attempt camera instance         c = camera.open();          //getting current parameters         camera.parameters params = c.getparameters();          //setting new parameters flash         params.setflashmode(parameters.flash_mode_torch);         c.setparameters(params);      }     catch (exception e){         // camera not available (in use or not exist)     }     // returns null if camera unavailable     return c;  }  /**generates delay needed application save new pictures */ private void delay(){     try {         //makes program inactive specific amout of time         thread.sleep(std_delay);     } catch (exception e) {         e.getstacktrace();     } }  /**method releasing camera on pause event*/ @override protected void onpause() {     super.onpause();     //shuts down preview shown on screen     mcamera.stoppreview();     //calls internal method restore camera     releasecamera();              }   /**help method release camera */ private void releasecamera(){     //checks if there camera object active     if (this.mcamera != null){         //releases camera         this.mcamera.release();         //restore camera object initial state         this.mcamera = null;     } }  /**activates camera , makes appear on screen */     protected void onresume() {     // todo auto-generated method stub     // deleting image external storage     filehandler.deletefromexternalstorage();     // create instance of camera.     this.mcamera = getcamerainstance();     // create our preview view , set content of our activity.     this.mpreview = new camerapreview(this, this.mcamera);     framelayout preview = (framelayout) findviewbyid(r.id.camera_preview);     preview.addview(this.mpreview);      //add capture button     addlisteneronbutton();     // in order receive data in jpeg format     this.mpicture = new picturecallback() {          /**creates file when image taken, if file doesn't exists*/         @override          public void onpicturetaken(byte[] data, camera mcamera) {          file picturefile = filehandler.getoutputmediafile(media_type_image);          if (picturefile == null){             log.d(tag, "error creating media file, check storage permissions");             return;         }          try {             //writes image disc             fileoutputstream fos = new fileoutputstream(picturefile);             fos.write(data);             fos.close();         } catch (filenotfoundexception e) {             log.d(tag, "file not found: " + e.getmessage());         } catch (ioexception e) {             log.d(tag, "error accessing file: " + e.getmessage());         }     } };     super.onresume(); }    } 

log cat:

05-21 14:32:05.424: d/openglrenderer(1030): enabling debug mode 0 05-21 14:32:10.986: e/cameraactivity(1030): camera not availablefail connect   camera service 05-21 14:32:11.033: i/choreographer(1030): skipped 66 frames!  application may doing work on main thread. 05-21 14:32:11.203: w/egl_emulation(1030): eglsurfaceattrib not implemented 05-21 14:32:13.013: d/androidruntime(1030): shutting down vm 05-21 14:32:13.013: w/dalvikvm(1030): threadid=1: thread exiting uncaught exception (group=0x40a71930) 05-21 14:32:13.083: e/androidruntime(1030): fatal exception: main 05-21 14:32:13.083: e/androidruntime(1030): java.lang.nullpointerexception 05-21 14:32:13.083: e/androidruntime(1030):     @ com.example.tensioncamapp_project.camerapreview.surfacecreated(camerapreview.java:33) 05-21 14:32:13.083: e/androidruntime(1030):     @ android.view.surfaceview.updatewindow(surfaceview.java:569) 05-21 14:32:13.083: e/androidruntime(1030):     @ android.view.surfaceview.access$000(surfaceview.java:86) 05-21 14:32:13.083: e/androidruntime(1030):     @ android.view.surfaceview$3.onpredraw(surfaceview.java:174) 05-21 14:32:13.083: e/androidruntime(1030):     @ android.view.viewtreeobserver.dispatchonpredraw(viewtreeobserver.java:680) 05-21 14:32:13.083: e/androidruntime(1030):     @ android.view.viewrootimpl.performtraversals(viewrootimpl.java:1842) 05-21 14:32:13.083: e/androidruntime(1030):     @ android.view.viewrootimpl.dotraversal(viewrootimpl.java:989) 05-21 14:32:13.083: e/androidruntime(1030):     @ android.view.viewrootimpl$traversalrunnable.run(viewrootimpl.java:4351) 05-21 14:32:13.083: e/androidruntime(1030):     @ android.view.choreographer$callbackrecord.run(choreographer.java:749) 05-21 14:32:13.083: e/androidruntime(1030):     @ android.view.choreographer.docallbacks(choreographer.java:562) 05-21 14:32:13.083: e/androidruntime(1030):     @ android.view.choreographer.doframe(choreographer.java:532) 05-21 14:32:13.083: e/androidruntime(1030):     @ android.view.choreographer$framedisplayeventreceiver.run(choreographer.java:735) 05-21 14:32:13.083: e/androidruntime(1030):     @ android.os.handler.handlecallback(handler.java:725) 05-21 14:32:13.083: e/androidruntime(1030):     @ android.os.handler.dispatchmessage(handler.java:92) 05-21 14:32:13.083: e/androidruntime(1030):     @ android.os.looper.loop(looper.java:137) 05-21 14:32:13.083: e/androidruntime(1030):     @    android.app.activitythread.main(activitythread.java:5041) 05-21 14:32:13.083: e/androidruntime(1030):     @    java.lang.reflect.method.invokenative(native method) 05-21 14:32:13.083: e/androidruntime(1030):     @ java.lang.reflect.method.invoke(method.java:511) 05-21 14:32:13.083: e/androidruntime(1030):     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 05-21 14:32:13.083: e/androidruntime(1030):     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 05-21 14:32:13.083: e/androidruntime(1030):     @ dalvik.system.nativestart.main(native method) 

the solution call getcamerainstancemethod() in onresume() in if-statement

protected void onresume() {     // todo auto-generated method stub     // deleting image external storage     filehandler.deletefromexternalstorage();     // create instance of camera.     if (this.mcamera == null){         this.mcamera = getcamerainstance();} 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -