title: android camera preview
body: <p>I have a problem.
  I'm trying to develop a program that uses the camera, everything is working in my device but like many of you know the <code>CameraPreview</code> doesn't work the same way in all devices, so I tried to integrate the code that google provides in this address:</p>
  
  <p><a href="http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/CameraPreview.html" rel="nofollow">http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/CameraPreview.html</a></p>
  
  <p>The problem I've faced is that I don't know how to integrate to my project, here is my code and what I've done without exceeding:</p>
  
  <pre><code>Preview mPreview;
  private Camera mCamera;
  
      @Override
  public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          mPreview = new Preview(this);
  
          //ContentView----------------------------------------------------------
          setContentView(R.layout.main);
  
          mSurfaceView = (SurfaceView)findViewById(R.id.camera_surface);
  
          mPreview.surfacemetod(mSurfaceView);
  
     @Override
     protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();    
  
        mCamera = Camera.open();
        mPreview.setCamera(mCamera);
  </code></pre>
  
  <p>Then the Preview class:</p>
  
  <pre><code>class Preview extends ViewGroup implements SurfaceHolder.Callback {
  private final String TAG = "Preview";
  
  SurfaceView mSurfaceView;
  SurfaceHolder mHolder;
  Size mPreviewSize;
  List&lt;Size&gt; mSupportedPreviewSizes;
  Camera mCamera;
  
  Preview(Context context) {
      super(context);
  
      mSurfaceView = new SurfaceView(context);
      addView(mSurfaceView);
  
  }
  
  public void surfacemetod(SurfaceView surface){
  
      //mSurfaceView = new SurfaceView(context);
      //addView(mSurfaceView);
  
      // Install a SurfaceHolder.Callback so we get notified when the
      // underlying surface is created and destroyed.
      mHolder = mSurfaceView.getHolder();
      mHolder.addCallback(this);
      mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  
  }
  
  public void setCamera(Camera camera) {
      mCamera = camera;
      if (mCamera != null) {
          mSupportedPreviewSizes = mCamera.getParameters().getSupportedPreviewSizes();
          requestLayout();
      }
  }
  </code></pre>
  
  <p>After this code there are all the methods in the link above that I didn't copy to save space.</p>
  
  <p>The question is that when I run the code the mPreviewSize.with and mPreviewSize.height</p>
  
  <pre><code>public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
      // Now that the size is known, set up the camera parameters and begin
      // the preview.
      Camera.Parameters parameters = mCamera.getParameters();
      parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
      requestLayout();
  
      mCamera.setParameters(parameters);
      mCamera.startPreview();
  }
  </code></pre>
  
  <p>are null and I get a FORCE CLOSE</p>
  
  <p>Essentially looks like it doesn't get inside OnMeasure to resolve them.
  Any idea?</p>
  
  <p>This is the log:</p>
  
  <pre><code>02-16 13:54:26.068: ERROR/AndroidRuntime(24374): FATAL EXCEPTION: main
  
  02-16 13:54:26.068: ERROR/AndroidRuntime(24374): java.lang.NullPointerException
  
  02-16 13:54:26.068: ERROR/AndroidRuntime(24374):     at com.dashboard.camera.lite.Preview.surfaceChanged(Preview.java:162)
  
  02-16 13:54:26.068: ERROR/AndroidRuntime(24374):     at android.view.SurfaceView.updateWindow(SurfaceView.java:554)
  
  02-16 13:54:26.068: ERROR/AndroidRuntime(24374):     at android.view.SurfaceView.dispatchDraw(SurfaceView.java:341)
  
  02-16 13:54:26.068: ERROR/AndroidRuntime(24374):     at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
  
  02-16 13:54:26.068: ERROR/AndroidRuntime(24374):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
  
  02-16 13:54:26.068: ERROR/AndroidRuntime(24374):     at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
  
  02-16 13:54:26.068: ERROR/AndroidRuntime(24374):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
  
  02-16 13:54:26.068: ERROR/AndroidRuntime(24374):     at android.view.View.draw(View.java:6796)
  
  02-16 13:54:26.068: ERROR/AndroidRuntime(24374):     at android.widget.FrameLayout.draw(FrameLayout.java:354)
  
  02-16 13:54:26.068: ERROR/AndroidRuntime(24374):     at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
  
  02-16 13:54:26.068: ERROR/AndroidRuntime(24374):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
  
  02-16 13:54:26.068: ERROR/AndroidRuntime(24374):     at android.view.View.draw(View.java:6796)
  
  02-16 13:54:26.068: ERROR/AndroidRuntime(24374):     at android.widget.FrameLayout.draw(FrameLayout.java:354)
  
  02-16 13:54:26.068: ERROR/AndroidRuntime(24374):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1894)
  
  02-16 13:54:26.068: ERROR/AndroidRuntime(24374):     at android.view.ViewRoot.draw(ViewRoot.java:1432)
  
  02-16 13:54:26.068: ERROR/AndroidRuntime(24374):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1174)
  
  02-16 13:54:26.068: ERROR/AndroidRuntime(24374):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1752)
  
  02-16 13:54:26.068: ERROR/AndroidRuntime(24374):     at android.os.Handler.dispatchMessage(Handler.java:99)
  
  02-16 13:54:26.068: ERROR/AndroidRuntime(24374):     at android.os.Looper.loop(Looper.java:123)
  
  02-16 13:54:26.068: ERROR/AndroidRuntime(24374):     at android.app.ActivityThread.main(ActivityThread.java:4627)
  
  02-16 13:54:26.068: ERROR/AndroidRuntime(24374):     at java.lang.reflect.Method.invokeNative(Native Method)
  
  02-16 13:54:26.068: ERROR/AndroidRuntime(24374):     at java.lang.reflect.Method.invoke(Method.java:521)
  
  02-16 13:54:26.068: ERROR/AndroidRuntime(24374):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
  
  02-16 13:54:26.068: ERROR/AndroidRuntime(24374):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  
  02-16 13:54:26.068: ERROR/AndroidRuntime(24374):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>