Android : NullPointerException accessing ZoomControls in custom camera zoom application

on Thursday, September 11, 2014


I am currently trying to add a zoom functionality to an existing and properly working custom camera application. Until now, I simply had a LinearLayout, which figured as holder for the camera preview.


Now I replaced that LinearLayout with a ZoomControls, and added code into the surfaceChanged function in order to handle zoom events. The problem is, that trying to access the ZoomControls results in a NullPointerException as soon as the Activity gets created (here thrown at Log.i("COSA", "some random test value " + zc.getBaseline());).



@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

if (holder.getSurface() == null){
// preview surface does not exist
return;
}

final Camera.Parameters params = camera.getParameters();
maxZoomLevel = params.getMaxZoom();
ZoomControls zc = (ZoomControls) findViewById(R.id.cam);
Log.i("COSA", "some random test value " + zc.getBaseline());

(...)


I am not quite sure why this error is thrown. The layout must be already existing via onCreate(), and I am initializing the camera in onStart(). Is there more work to do than simply replace that LinearLayout with a ZoomControls?


This is the LogCat output:



09-11 11:59:07.374: E/AndroidRuntime(14497): FATAL EXCEPTION: main
09-11 11:59:07.374: E/AndroidRuntime(14497): java.lang.NullPointerException
09-11 11:59:07.374: E/AndroidRuntime(14497): at com.unibas.projects.cosa.android.CustomCameraPreview.surfaceChanged(CustomCameraPreview.java:67)
09-11 11:59:07.374: E/AndroidRuntime(14497): at android.view.SurfaceView.updateWindow(SurfaceView.java:620)
09-11 11:59:07.374: E/AndroidRuntime(14497): at android.view.SurfaceView.access$000(SurfaceView.java:86)
09-11 11:59:07.374: E/AndroidRuntime(14497): at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:178)
09-11 11:59:07.374: E/AndroidRuntime(14497): at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:707)
09-11 11:59:07.374: E/AndroidRuntime(14497): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1946)
09-11 11:59:07.374: E/AndroidRuntime(14497): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1112)
09-11 11:59:07.374: E/AndroidRuntime(14497): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4518)
09-11 11:59:07.374: E/AndroidRuntime(14497): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
09-11 11:59:07.374: E/AndroidRuntime(14497): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
09-11 11:59:07.374: E/AndroidRuntime(14497): at android.view.Choreographer.doFrame(Choreographer.java:525)
09-11 11:59:07.374: E/AndroidRuntime(14497): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
09-11 11:59:07.374: E/AndroidRuntime(14497): at android.os.Handler.handleCallback(Handler.java:615)
09-11 11:59:07.374: E/AndroidRuntime(14497): at android.os.Handler.dispatchMessage(Handler.java:92)
09-11 11:59:07.374: E/AndroidRuntime(14497): at android.os.Looper.loop(Looper.java:137)
09-11 11:59:07.374: E/AndroidRuntime(14497): at android.app.ActivityThread.main(ActivityThread.java:4898)
09-11 11:59:07.374: E/AndroidRuntime(14497): at java.lang.reflect.Method.invokeNative(Native Method)
09-11 11:59:07.374: E/AndroidRuntime(14497): at java.lang.reflect.Method.invoke(Method.java:511)
09-11 11:59:07.374: E/AndroidRuntime(14497): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
09-11 11:59:07.374: E/AndroidRuntime(14497): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
09-11 11:59:07.374: E/AndroidRuntime(14497): at dalvik.system.NativeStart.main(Native Method)


Note: there are many questions concerning NullPointerExceptions in custom camera zoom related issues, but none of them refer to the access of ZoomControls.


0 comments:

Post a Comment