Android Camera.autoFocus() failed

eclipseWhile coding a camera test program I run into the following problem… Camera.autoFocus(autoFocusCallback) failed the whole time with the error message „autoFocus failed“. The stacktrace contains:

java.lang.RuntimeException: autoFocus failed
at android.hardware.Camera.native_autoFocus

The internet wasn’t a real help on this topic. So I had to dig deeper on my own.
It was a bit tricky to find the error since I used a filter on my application in the logcat output. So I saw only the „autoFocus failed“ message. When I switched to „All messages“ I saw the camera is complaining that there is no active preview. Huh? But I saw the camera preview on the phone. Yeah, but this preview was created some milliseconds later than the first autoFocus request.

I use the SurfaceHolder.Callback -> surfaceCreated now to know when the surfaceHolder is created. After this I can start the AutoFocus.

Everything runs fine now 🙂