I have searched some solutions with my problem and sure there are many related issues regarding it but nothing solves my concern.

I am receiving a runtime exception : `takepicture failed:native_autofocus`..etc.

I tried to take picture from camera using autofocus and I can't seem to understand what could have caused the error. 

Here is my code.

```
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder mholder;
    private Camera mcamera;
    private Handler handler = new Handler();
    public CameraPreview(Context context, Camera camera) {
        super(context);
        this.mcamera = camera;
        mholder = getHolder();
        mholder.addCallback(this);
        mholder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        if (mholder.getSurface() == null) {
            return;
        }
        try {
            mcamera.stopPreview();
        } catch (Exception e) {
            Log.d("surfaceChanged", e.toString());
        }
        try {
            mcamera.setPreviewDisplay(holder);
            mcamera.startPreview();           
        } catch (IOException e) {
            Log.d("surfaceChanged--->surfaceCreated", e.toString());
        }
    }

```

I think somethings is lacking in my runnable code, I tried to omit the runnable and execute autofocus once, it removed the runtime error. Did I missed to reinitialize something in this part?

```
    private void autoFocus(Camera mcamera){
        final  Camera cam=mcamera;
        handler.postDelayed(new Runnable() {            
            @Override
            public void run() {         
                cam.autoFocus(autoFocusCallback);
                handler.postDelayed(this, 1500L);            
            }
        }, 1500L);
    }
    AutoFocusCallback autoFocusCallback=new AutoFocusCallback() {

        @Override
        public void onAutoFocus(boolean success, Camera camera) {
            camera.takePicture(null, null, mPicture);
            }
    };
    private PictureCallback mPicture=new PictureCallback() {

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
          //do something
        }
    };

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        try {
            mcamera.setPreviewDisplay(holder);
            mcamera.startPreview();
            mcamera.autoFocus(autoFocusCallback);
        } catch (IOException e) {
            Log.d("surfaceCreated", e.toString());
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }
}

```