-1

after i changed my device my camera suddently stopped working and throwing out a

java.lang.NullPointerException: Attempt to invoke virtual method 'android.hardware.Camera$Parameters android.hardware.Camera.getParameters()' on a null object reference

i had tryed to change out my catch (RuntimeException ex) to finaly but that instead gaved me a

Fail to connect to camera service” exception

public void surfaceCreated(SurfaceHolder surfaceHolder) {
    String PreviewFPS;
    String Previewsize;
    String Displayor;
    PreviewFPS = setingPreferences.getString("previewfps", "");
    Previewsize = setingPreferences.getString("screensize", "");
    Displayor = setingPreferences.getString("orientation", "");
    String[] size = Previewsize.split(",");
    try {
        camera = Camera.open();
    } catch (RuntimeException ex) {
    }
    Camera.Parameters parameters;
  parameters = camera.getParameters();

    //modificer parameterene
          parameters.setPreviewFrameRate(Integer.parseInt(PreviewFPS));
        parameters.setPreviewSize(Integer.parseInt(size[0]),Integer.parseInt(size[1]));
     camera.setParameters(parameters);
    parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
   camera.setDisplayOrientation(Integer.parseInt(Displayor));
    try {
        camera.setPreviewDisplay(surfaceHolder);
        camera.startPreview();
    } catch (Exception ex) {
    }

}

manifest

 <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />

the manufactor of the device had investigated the code and told me that the reason was due to it couldn't run the scanner and camera at once

Hi Kewin   Would you please share the source code to me? as we haven't tested if it can run both the camera and scanner  

We checked, the scanner and camera can not be opened in same time.   Best Regards,

so my fix for this was by having my camera and scanner activity running in 2 separate activites and layouts hopefully this would help someone else

1 Answers1

0

You need all the camera code in the try and you should read the stacktraces if you actually printed them

try {
    camera = Camera.open();
    Camera.Parameters parameters = camera.getParameters();

   ... 
} catch (RuntimeException ex) {
    ex.printStackTrace();
}

// camera remains null if an exception is caught... 
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245