Unable start camera Preview on button click
<p>I'm a newbie programmer for Android I was trying to start camera preview in Android on click of button. I'm trying directly to access camera hardware rather than using default camera application. Here is the code I've written</p>

<pre><code>import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class CameraHandler extends Activity {
    private SurfaceView sv = null;
    private SurfaceHolder previewHolder = null;

    private Button btnStop;
    private Button btnStart;
    private Camera camera = null;

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cameraview);

    // Get the surface view from main layout
    sv = (SurfaceView) findViewById(R.id.cameraSurface);

    // Get the buttons from XML
    btnStart = (Button) findViewById(R.id.startButton);
    btnStop = (Button) findViewById(R.id.stopButton);



    btnStart.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
        // TODO Auto-generated method stub

        SurfaceHolder.Callback surfaceCallBack = new SurfaceHolder.Callback() {

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
            // TODO Auto-generated method stub

            camera.release();
            camera = null;
            }

            @Override
            public void surfaceCreated(SurfaceHolder holder) {
            // TODO Auto-generated method stub
            camera = Camera.open();
            try {
                camera.setPreviewDisplay(previewHolder);
            } catch (Throwable t) {
                // TODO Auto-generated catch block
                Log.e("PreviewDemo-surfaceCallback",
                    "Exception in setPreviewDisplay()", t);
                Toast.makeText(CameraHandler.this, t.getMessage(),
                    Toast.LENGTH_LONG).show();
            }
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder,
                int format, int width, int height) {
            // TODO Auto-generated method stub
            Camera.Parameters parameters = camera.getParameters();
            parameters.setPreviewSize(width, height);
            camera.setParameters(parameters);
            camera.startPreview();

            }
        };

        // Get the surface holder from the surface view
        previewHolder = sv.getHolder();

        // Now add these call backs
        previewHolder.addCallback(surfaceCallBack);
        previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        }
    });

    btnStop.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
        // TODO Auto-generated method stub
        camera.stopPreview();
        }
    });
    }

}
</code></pre>

<p>As you can see I'm getting surface holder from the SurfaceView in XML adding it a call back when start button is clicked. For some reason SufraceHolder callback is not getting called. But If i place this entire code directly in onCreate function it works fine i.e camera preview starts as activity is launched. Am I missing something here? </p>

<p>Thanks in Advance</p>
