I have built a custom camera application . Facing a problem in screen orientation and I don't want the activity to be re-created when the orientation changes. Please help me on this with the complete solution.
Getting a NULL pointer exception inside the surfaceChanged method in the preview class.
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if (mCamera != null) {
Camera.Parameters parameters = mCamera.getParameters();
Log.d("SurfaceChanged", "getting parameters" + parameters);
Getting a NULL pointer exception in the below line :
Display display = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
Log.d("SurfaceChanged", "Getting the system services");
if(display.getRotation() == Surface.ROTATION_0){
Log.d("SurfaceChanged", "Inside the if condition 1");
parameters.setPreviewSize(height,width);
Log.d("SurfaceChanged", "setting preview size");
mCamera.setDisplayOrientation(90);
Log.d("SurfaceChanged", "setting display orientation as 90");
}
if(display.getRotation() == Surface.ROTATION_90){
Log.d("SurfaceChanged", "Inside the if condition 2");
parameters.setPreviewSize(width,height);
Log.d("SurfaceChanged", "setting the preview size 2 ");
}
if(display.getRotation() == Surface.ROTATION_180){
Log.d("SurfaceChanged", "Inside the if condition 3");
parameters.setPreviewSize(height,width);
Log.d("SurfaceChanged", "setting the preview size 3");
}
if(display.getRotation() == Surface.ROTATION_270){
Log.d("SurfaceChanged", "Inside the if condition 4");
parameters.setPreviewSize(width,height);
Log.d("SurfaceChanged", "setting preview size 4");
mCamera.setDisplayOrientation(180);
Log.d("SurfaceChanged", "setting display orientation as 180");
}
mCamera.setParameters(parameters);
Log.d("SurfaceChanged", "setting the parameters");
try {
mCamera.setPreviewDisplay(mHolder);
Log.d("SurfaceChanged", "setting preview display to mHolder");
mCamera.startPreview();
Log.d("SurfaceChanged", "preview as started");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Any help would be greatly appreciated.
Here is the error log:
12-23 16:07:34.962 8845-8845/com.clarity.camera E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.clarity.camera , PID: 8845 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference at com.clarity.camera.CameraPreview.surfaceChanged(CameraPreview.java:230) at android.view.SurfaceView.updateWindow(SurfaceView.java:583) at android.view.SurfaceView.access$000(SurfaceView.java:86) at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:175) at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:847) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1897) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1019) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5725) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761) at android.view.Choreographer.doCallbacks(Choreographer.java:574) at android.view.Choreographer.doFrame(Choreographer.java:544) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5086) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)