surfaceCreated is not called

18,274

Solution 1

When you declare SurfaceView in the layout you tell Android to use built-in class. What you need to do is make it use your class by replacing SurfaceView in XML with your.package.name.CameraPreview.

One other thing: you should add a constructor to the CameraPreview taking parameters Context and AttributeSet, otherwise layout inflater won't be able to inflate your class. Also you don't need the line cameraPreview = new CameraPreview(this);.

Solution 2

When your onCreate() is the right one, than you overriding the cameraPreview from the xml with a new created one which you never add to any layout (last line). That means its never inflated so there is never a surface created.

Share:
18,274
Hans Muff
Author by

Hans Muff

Updated on July 19, 2022

Comments

  • Hans Muff
    Hans Muff almost 2 years

    I'm trying to put a camera preview (SurfaceView) together with a button on the display, but all I get is a blank screen only showing the button. If I'm setting the SurfaceView as the only content (with setContentView(surfaceView) then the preview is displaying fine on the screen. What am I doing wrong?

    Thanks in advance

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:orientation="vertical">
        <FrameLayout
            android:layout_width="fill_parent"
            android:layout_height="0px"
            android:layout_weight="1">
            <SurfaceView
                android:id="@+id/camera_surface"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" />
            <Button 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:text="@string/trigger"
                android:id="@+id/trigger_picture_button" 
                android:layout_gravity="bottom" />
        </FrameLayout>
    </LinearLayout>
    

    My Activity:

    public class CameraActivity extends Activity {
    
        private SurfaceView cameraPreview;
        private Button triggerPicture;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.camera);
    
            cameraPreview = (SurfaceView) findViewById(R.id.camera_surface);
            triggerPicture = (Button) findViewById(R.id.trigger_picture_button);
    
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            cameraPreview = new CameraPreview(this);
    //      setContentView(cameraPreview);
        }
    

    My CameraPreview:

    public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    
    private SurfaceHolder holder;
    private Camera camera;
    
    public CameraPreview(Context context) {
        super(context);
        holder = getHolder();
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }
    
    public void surfaceCreated(SurfaceHolder holder) {
        camera = openFrontFacingCamera();
        try {
            camera.setPreviewDisplay(holder);
        } catch (IOException e) {
            camera.release();
            camera = null;
        }
    }
    
    public void surfaceDestroyed(SurfaceHolder holder) {
        camera.stopPreview();
        camera.release();
        camera = null;
    }
    
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        Camera.Parameters parameters = camera.getParameters();
        parameters.setPreviewSize(width, height);
        parameters.set("orientation", "portrait");
        camera.setParameters(parameters);
        camera.startPreview();
    }
    
    private Camera openFrontFacingCamera() {
        // returns Camera.open(
    }
    
  • Hans Muff
    Hans Muff about 13 years
    Thank you, Malcolm. I tried naming my CameraPreview class in the xml before, but it didn't help. I solved my problem using your second advice. As I removed the line cameraPreview = new CameraPreview(this), the preview started working. Good job :)
  • RichieHH
    RichieHH almost 10 years
    this has nothing whatsoever to do with surfaceView.