Reputation: 1977
I'm following the Camera guide on Android developers. However when I try to run the activity I get a Java lang NullPointer Exception which I don't understand where it's coming from.
This is the Camera activity I wrote down.
public class Call extends Activity {
private Camera mCamera;
private CameraPreview mPreview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_call);
// Create an instance of Camera
mCamera = getCameraInstance();
System.out.println(Camera.open());
// Create our Preview view and set it as the content of our activity.
mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.start_call, menu);
return true;
}
/** Check if this device has a camera */
private boolean checkCameraHardware(Context context) {
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
// this device has a camera
System.out.println("Has a camera it has "+Camera.getNumberOfCameras());
getCameraInstance();
return true;
} else {
// no camera on this device
System.out.println("Does not have a camera");
return false;
}
}
/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
System.out.println("Got the camera");
}
catch (Exception e){
// Camera is not available (in use or does not exist)
System.out.println("Not available");
}
return c; // returns null if camera is unavailable
}
}
and this is the camerapreview class
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the preview.
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
Log.d("error", "Error setting camera preview: " + e.getMessage());
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your activity.
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.
if (mHolder.getSurface() == null){
// preview surface does not exist
return;
}
// stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception e){
// ignore: tried to stop a non-existent preview
}
// set preview size and make any resize, rotate or
// reformatting changes here
// start preview with new settings
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e){
Log.d("error", "Error starting camera preview: " + e.getMessage());
}
}
}
This is the error I got.
07-24 17:55:11.856: E/AndroidRuntime(796): FATAL EXCEPTION: main
07-24 17:55:11.856: E/AndroidRuntime(796): java.lang.NullPointerException
07-24 17:55:11.856: E/AndroidRuntime(796): at com.reflap.reflap.CameraPreview.surfaceCreated(CameraPreview.java:31)
07-24 17:55:11.856: E/AndroidRuntime(796): at android.view.SurfaceView.updateWindow(SurfaceView.java:543)
07-24 17:55:11.856: E/AndroidRuntime(796): at android.view.SurfaceView.access$000(SurfaceView.java:81)
07-24 17:55:11.856: E/AndroidRuntime(796): at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:169)
07-24 17:55:11.856: E/AndroidRuntime(796): at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:671)
07-24 17:55:11.856: E/AndroidRuntime(796): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1820)
07-24 17:55:11.856: E/AndroidRuntime(796): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
07-24 17:55:11.856: E/AndroidRuntime(796): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
07-24 17:55:11.856: E/AndroidRuntime(796): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
07-24 17:55:11.856: E/AndroidRuntime(796): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
07-24 17:55:11.856: E/AndroidRuntime(796): at android.view.Choreographer.doFrame(Choreographer.java:525)
07-24 17:55:11.856: E/AndroidRuntime(796): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
07-24 17:55:11.856: E/AndroidRuntime(796): at android.os.Handler.handleCallback(Handler.java:615)
07-24 17:55:11.856: E/AndroidRuntime(796): at android.os.Handler.dispatchMessage(Handler.java:92)
07-24 17:55:11.856: E/AndroidRuntime(796): at android.os.Looper.loop(Looper.java:137)
07-24 17:55:11.856: E/AndroidRuntime(796): at android.app.ActivityThread.main(ActivityThread.java:4745)
07-24 17:55:11.856: E/AndroidRuntime(796): at java.lang.reflect.Method.invokeNative(Native Method)
07-24 17:55:11.856: E/AndroidRuntime(796): at java.lang.reflect.Method.invoke(Method.java:511)
07-24 17:55:11.856: E/AndroidRuntime(796): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-24 17:55:11.856: E/AndroidRuntime(796): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-24 17:55:11.856: E/AndroidRuntime(796): at dalvik.system.NativeStart.main(Native Method)
07-24 17:55:15.045: I/Process(796): Sending signal. PID: 796 SIG: 9
07-24 17:55:15.956: E/Trace(817): error opening trace file: No such file or directory (2)
I don't know where the null is coming from. I did print out this System.out.println(Camera.open()); and it printed out null which was weird but the error stack points to this, mCamera.setPreviewDisplay(holder);
I'm running it on the android emulator but I turned on the camera and tested it in the camera app which worked.
Upvotes: 2
Views: 1542
Reputation: 1103
override the ondestroy() method and release your camera instance mcamera.release() reference link http://developer.android.com/training/camera/cameradirect.html
Upvotes: 0
Reputation: 2881
You have no code to test you have a camera. Make sure you have a camera before doing anything else with it.
In this code I am looking for the only camera or the camera facing the back ...
public int getCameraId() {
Log.d(TAG, "getCameraId()");
int cameraId = -1;
// Search for the back facing camera (or any camera)
int numberOfCameras = Camera.getNumberOfCameras();
for (int i = 0; i < numberOfCameras; i++) {
CameraInfo info = new CameraInfo();
Camera.getCameraInfo(i, info);
if (info.facing == CameraInfo.CAMERA_FACING_BACK || numberOfCameras == 1) {
Log.d(TAG, "CameraInfo.CAMERA_FACING_BACK = "
+ (info.facing == CameraInfo.CAMERA_FACING_BACK));
cameraId = i;
break;
}
}
return cameraId;
}
public static Camera getCameraInstance(int cameraId) {
Log.d(TAG, "getCameraInstance("+cameraId+")");
Camera c = null;
try {
c = Camera.open(cameraId); // attempt to get a Camera instance
Camera.Parameters cp = c.getParameters();
Log.d(TAG, "getCameraInstance("+cameraId+"): Camera.Parameters = "
+ cp.flatten());
} catch (Exception e) {
Log.d(TAG, "Camera.open("+cameraId+") exception="+e);
}
Log.d(TAG, "getCameraInstance("+cameraId+") = "+c);
return c; // returns null if camera is unavailable
}
The calling checks that cameraId
returned from getCameraId
is > -1 before calling getCameraInstance
.
Upvotes: 2