Note that there are some explanatory texts on larger screens.

plurals
  1. POCamera is null when the surfaceCreated method is executed for second time
    text
    copied!<p>I have a problem with the camera object when the surfaceCreated method of a SurfaceHolder.Callback is called for second time, I mean:</p> <p>I create an object camera in my onResume method from my activity, and this works fine showing the preview display, but when my activity goes to pause the surfaceview is destroyed and I have to release the camera object then if my activity comes to onresume android throws me a nullPointerException in my camera object.</p> <p>I'm not sure why is happening this, I putted comments to debug every method and see what happens, apparently everything is fine only in the surfaceCreated method the camera object becomes to null.</p> <p>This is my class:</p> <pre><code>public class CameraRecord implements SurfaceHolder.Callback{ public static final int BACK_CAMERA = 1; public static final int FRONT_CAMERA = 2; //private SurfaceView surface; private SurfaceHolder holder; private Camera camera; public CameraRecord(SurfaceView surface){ // this.surface = surface; holder = surface.getHolder(); holder.addCallback(this); holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); Log.e("CameraRecord","constructor"); } public void openCamera(int wichCamera) throws Exception { if (wichCamera == BACK_CAMERA) camera = Camera.open(); else { Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); int cameraCount = Camera.getNumberOfCameras(); for (int i = 0; i &lt; cameraCount; i++ ) { Camera.getCameraInfo(i, cameraInfo); if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) try { camera = Camera.open(i); Log.e("CameraRecord","camera is CAMERA_FACING_FRONT " + cameraInfo.toString()); } catch (RuntimeException e) { e.printStackTrace(); } } } if (camera == null) Log.e("CameraRecord","openCamera camera is null"); else Log.e("CameraRecord","openCamera camera is not null"); } public void start() throws IOException { camera.startPreview(); } public void stop() { camera.stopPreview(); Log.e("CameraRecord","stop camera"); } public void release() { camera.release(); camera = null; } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { Log.e("CameraRecord", "surfaceChanged"); try { start(); } catch (IOException e) { e.printStackTrace(); } } @Override public void surfaceCreated(SurfaceHolder holder) { // if (holder == null) // Log.e("CameraRecord","holder is null"); // else // Log.e("CameraRecord","holder is not null"); try { if (camera == null) Log.e("CameraRecord","camera is null"); else Log.e("CameraRecord","camera is not null"); camera.setPreviewDisplay(holder); // start(); } catch (IOException e) { e.printStackTrace(); } } @Override public void surfaceDestroyed(SurfaceHolder holder) { Log.e("CameraRecord", "surfaceDestroyed"); stop(); release(); } </code></pre> <p>}</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload