title: Organizing methods between SurfaceView and Activity
body: <p>For two days I have been trying to show the camera in a view. I followed many tutorials like these two:</p>
  
  <p><a href="http://code.google.com/p/openmobster/wiki/CameraTutorial" rel="nofollow noreferrer">http://code.google.com/p/openmobster/wiki/CameraTutorial</a>
  <a href="http://developer.android.com/training/camera/cameradirect.html" rel="nofollow noreferrer">http://developer.android.com/training/camera/cameradirect.html</a></p>
  
  <p>but without success to show the camera on the screen.</p>
  
  <p>I finally found <a href="http://android-er.blogspot.com/2010/12/camera-preview-version-ii.html" rel="nofollow noreferrer">this</a> which works directly. Just had to remove one deprecated method.
  From that one I tried to build something similar to the example in the two first sites, but I cannot get it working.</p>
  
  <p>At this line: </p>
  
  <pre class="lang-java prettyprint-override"><code>surfaceView = (PreView)findViewById(R.id.camerapreview);
  </code></pre>
  
  <p>I tried to modify it by:</p>
  
  <pre class="lang-java prettyprint-override"><code>surfaceView = new PreView(this);
        FrameLayout layout = (FrameLayout)findViewById(R.id.camerapreview); 
        layout.addView(surfaceView);
  </code></pre>
  
  <p>But it does not work. Then I also changed the <code>SurfaceView</code> by <code>LinearLayout</code> in the <code>activity_main.xml</code>. I did not work. The constructor of <code>PReView</code> did not even run (I put a breakpoint in it, but the bug is before that).
  Do I need to have a nested layout to get it working? I tried many things. I hope someone will have a look:</p>
  
  <p><strong>MainActivity.java:</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>package com.example.cameratest2;
  import android.app.Activity;
  import android.content.pm.ActivityInfo;
  import android.graphics.PixelFormat;
  import android.os.Bundle;
  
  public class MainActivity extends Activity{
  PreView surfaceView;
  
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
  
        getWindow().setFormat(PixelFormat.UNKNOWN);
        surfaceView = (PreView)findViewById(R.id.camerapreview);
        //surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }
  }
  </code></pre>
  
  <p><strong>Preview.java:</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>package com.example.cameratest2;
  
  import java.io.IOException;
  
  import android.content.Context;
  import android.hardware.Camera;
  import android.view.SurfaceHolder;
  import android.view.SurfaceView;
  
  public class PreView extends SurfaceView implements SurfaceHolder.Callback {
  Camera camera;
  boolean previewing = false;
  SurfaceHolder surfaceHolder;
  
  public PreView(Context context) {
      super(context);
  
      surfaceHolder = this.getHolder();
      surfaceHolder.addCallback(this);
  }
  
  @Override
  public void surfaceChanged(SurfaceHolder holder, int format, 
      int width,int height)   {
      if(previewing){
       camera.stopPreview();
       previewing = false;
      }
  
      if (camera != null){
       try {
        camera.setPreviewDisplay(surfaceHolder);
        camera.startPreview();
        previewing = true;
       } catch (IOException e) {
        e.printStackTrace();
       }
      }
  }
  
  @Override
  public void surfaceCreated(SurfaceHolder holder) {
      camera = Camera.open();
  }
  
  @Override
  public void surfaceDestroyed(SurfaceHolder holder) {
      camera.stopPreview();
      camera.release();
      camera = null;
      previewing = false;
  }
    }
  </code></pre>
  
  <p><strong>activity_main.xml</strong></p>
  
  <pre class="lang-xml prettyprint-override"><code> &lt;?xml version="1.0" encoding="utf-8"?&gt;
   &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   &gt;
   &lt;SurfaceView
   android:id="@+id/camerapreview"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   /&gt;
   &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>the logCat:</strong></p>
  
  <pre><code>    11-09 04:22:58.856: W/dalvikvm(1612): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
      11-09 04:22:58.975: E/AndroidRuntime(1612): FATAL EXCEPTION: main
      11-09 04:22:58.975: E/AndroidRuntime(1612): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cameratest2/com.example.cameratest2.MainActivity}: java.lang.ClassCastException: android.view.SurfaceView cannot be cast to com.example.cameratest2.PreView
      11-09 04:22:58.975: E/AndroidRuntime(1612):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
      11-09 04:22:58.975: E/AndroidRuntime(1612):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
      11-09 04:22:58.975: E/AndroidRuntime(1612):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
      11-09 04:22:58.975: E/AndroidRuntime(1612):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
      11-09 04:22:58.975: E/AndroidRuntime(1612):     at android.os.Handler.dispatchMessage(Handler.java:99)
      11-09 04:22:58.975: E/AndroidRuntime(1612):     at android.os.Looper.loop(Looper.java:137)
      11-09 04:22:58.975: E/AndroidRuntime(1612):     at android.app.ActivityThread.main(ActivityThread.java:4745)
      11-09 04:22:58.975: E/AndroidRuntime(1612):     at java.lang.reflect.Method.invokeNative(Native Method)
      11-09 04:22:58.975: E/AndroidRuntime(1612):     at java.lang.reflect.Method.invoke(Method.java:511)
      11-09 04:22:58.975: E/AndroidRuntime(1612):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
      11-09 04:22:58.975: E/AndroidRuntime(1612):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
      11-09 04:22:58.975: E/AndroidRuntime(1612):     at dalvik.system.NativeStart.main(Native Method)
      11-09 04:22:58.975: E/AndroidRuntime(1612): Caused by: java.lang.ClassCastException: android.view.SurfaceView cannot be cast to com.example.cameratest2.PreView
      11-09 04:22:58.975: E/AndroidRuntime(1612):     at com.example.cameratest2.MainActivity.onCreate(MainActivity.java:19)
      11-09 04:22:58.975: E/AndroidRuntime(1612):     at android.app.Activity.performCreate(Activity.java:5008)
      11-09 04:22:58.975: E/AndroidRuntime(1612):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
      11-09 04:22:58.975: E/AndroidRuntime(1612):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
      11-09 04:22:58.975: E/AndroidRuntime(1612):     ... 11 more
  </code></pre>
  
  <p><strong>[edit]</strong></p>
  
  <p>I found this : <a href="https://stackoverflow.com/questions/5203547/draw-surfaceview-from-layout-xml">Draw SurfaceView from layout xml</a>. <br/>As explained in this link, I made a modification in my <code>activity_main.xml</code> replacing <code>&lt;SurfaceView&gt;</code> with <code>&lt;com.example.cameratest2.PreView&gt;</code> and modifying the MainActivity.java to replace </p>
  
  <pre><code> surfaceView = (PreView)findViewById(R.id.camerapreview);
  </code></pre>
  
  <p>with </p>
  
  <pre><code> surfaceView = new PreView(this);
  </code></pre>
  
  <p>...but it still does not work, and I get this error:</p>
  
  <pre><code>11-09 05:01:14.114: E/AndroidRuntime(1700): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class com.example.cameratest2.PreView
  </code></pre>