I\'m trying to code an app that uses the camera. I\'m getting a NullPointerException when trying to get the surfaceHolder that i eventually pass to the surfaceCreated() that
Starting a new answer to hopefully be more clear. Again, I think the root issue is the amount of stuff youre doing in onCreate vs. onResume.
I think the root issue youre having is that some parts of the view/activity lifecycle need to run their course before you start dealing with the surfaceView and starting prevew with it.
I'm not certain of that but here's the breakdown of the various places I deal with various camera and surface view stuff in an augmented reality app that works:
onCreate()
{
// just set content view. do nothing with the camera or surfaceView yet
setContentView(R.layout.main);
}
onResume()
{
// open camera
mCamera = Camera.open();
// init surface view
sv = (SurfaceView)this.findViewById(R.id.SurfaceView01);
mHolder = sv.getHolder();
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mHolder.setSizeFromLayout();
mHolder.addCallback(this);
}
surfaceChanged(SurfaceHolder holder, int format, int w, int h)
{
mCamera.setPreviewDisplay(holder);
// set any cam params you need...
mCamera.startPreview();
}
Try restructuring your code to do similar. My eyes got bleary trying to count out where the NPE was happening from your source but I think a few problems are:
Hope that helps. We'll get this figured out yet!
Off the top of my head, I think you'll need to do the surfaceView stuff in onResume rather than in onCreate, otherwise it looks fine.
Also, you should try calling:
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mSurfaceHolder.setSizeFromLayout();
right after you get a ref to the holder, but before starting preview.