SurfaceView - > Surface had no valid native window | Android Camera API | hardware.camera2
<p>I'm having troubles with the SurfaceView class of Android Developing using it on the latest Camera API. I've double checked that my min API level is 21. </p>

<p>I've approached this in two ways, one is creating the SurfaceView in the XML and then giving the value of it to my private SurfaceView or creating it with the public constructor.</p>

<p>With that SurfaceView I can obtain the SurfaceHolder with getHolder() and then the Surface with getSurface().</p>

<p>In the code I'm posting I'm just creating it with the public constructor.</p>

<p>This SurfaceView is supposed to put the preview of the Camera with <a href="https://developer.android.com/reference/android/hardware/camera2/CameraDevice.html#createCaptureSession(java.util.List%3Candroid.view.Surface%3E,%20android.hardware.camera2.CameraCaptureSession.StateCallback,%20android.os.Handler)" rel="nofollow noreferrer">createCaptureSession()</a> </p>

<p>Here's my code, <em>its a quick class implementation I've tried after trying it yesterday with another project</em>. check onCreate() of MainActivity and onOpened(), there's where SurfaceView is used.</p>

<pre><code>package com.example.universitywork.canyouwork;
/* IMPORTS */ 

public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback {
// CREAMOS CLASES
private CameraManager mManager;
private CameraDevice cDevice;
private CaptureRequest cRequest;
private Handler handle = new Handler();
private SurfaceView surface;
private SurfaceHolder holder;
private Surface under;
private final CameraDevice.StateCallback mCallbackDevice = new CameraDevice.StateCallback() {

    @Override
    public void onOpened(CameraDevice camera) {
        cDevice = camera;
        try {
            camera.createCaptureSession(Arrays.asList(under), mCallbackSession, handle);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onDisconnected(CameraDevice camera) {
        System.out.println("ONDC");
    }

    @Override
    public void onError(CameraDevice camera, int error) {
        System.out.println("ONerror");
    }
};

private final CameraCaptureSession.CaptureCallback mCaptureCallback = new CameraCaptureSession.CaptureCallback() {

};

private final CameraCaptureSession.StateCallback mCallbackSession = new CameraCaptureSession.StateCallback() {

    @Override
    public void onConfigured(CameraCaptureSession session) {
        try {
            System.out.println("LLEGO AQUI");

            cRequest = cDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW).build();
          //  session.capture(cRequest, mCaptureCallback, handle);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onConfigureFailed(CameraCaptureSession session) {
        System.out.println("ONCONFIGUREFAILED");
    }
};

public MainActivity() {

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    surface= new SurfaceView(this);
    holder = surface.getHolder();
    holder.addCallback(this);
    holder.setFixedSize(500,500);
    under= holder.getSurface();
    mManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
    String id = getId(mManager);
    try {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {

            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        mManager.openCamera(id, mCallbackDevice, handle);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}


@Override
public void surfaceCreated(SurfaceHolder holder) {
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    System.out.println("ONCHANGED");
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    System.out.println("ONDESTROYED");
}
public String getId (CameraManager manager) {
    try {
        for (String cameraId : manager.getCameraIdList()) {
            CameraCharacteristics characteristics =         manager.getCameraCharacteristics(cameraId);
            int orientation =     characteristics.get(CameraCharacteristics.LENS_FACING);
            if (orientation == CameraCharacteristics.LENS_FACING_FRONT) return cameraId;
        }
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
    return "NULL";
}}
</code></pre>

<p>Fast forward to the error:</p>

<pre><code>    E/Legacy-CameraDevice-JNI: getNativeWindow: Surface had no valid native window.
    E/Legacy-CameraDevice-JNI: LegacyCameraDevice_nativeDetectSurfaceDimens: Could not retrieve native window from surface.
    D/AndroidRuntime: Shutting down VM
    E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.universitywork.canyouwork, PID: 4885
                  java.lang.IllegalArgumentException: Surface was abandoned
                      at android.hardware.camera2.utils.SurfaceUtils.getSurfaceSize(SurfaceUtils.java:70)
                      at android.hardware.camera2.params.OutputConfiguration.&lt;init&gt;(OutputConfiguration.java:97)
                      at android.hardware.camera2.params.OutputConfiguration.&lt;init&gt;(OutputConfiguration.java:71)
                      at android.hardware.camera2.impl.CameraDeviceImpl.createCaptureSession(CameraDeviceImpl.java:474)
                      at com.example.universitywork.canyouwork.MainActivity$1.onOpened(MainActivity.java:40)
                      at android.hardware.camera2.impl.CameraDeviceImpl$1.run(CameraDeviceImpl.java:134)
                      at android.os.Handler.handleCallback(Handler.java:739)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:148)
                      at android.app.ActivityThread.main(ActivityThread.java:5417)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                   Caused by: android.hardware.camera2.legacy.LegacyExceptionUtils$BufferQueueAbandonedException
                      at android.hardware.camera2.legacy.LegacyExceptionUtils.throwOnError(LegacyExceptionUtils.java:64)
                      at android.hardware.camera2.legacy.LegacyCameraDevice.getSurfaceSize(LegacyCameraDevice.java:540)
                      at android.hardware.camera2.utils.SurfaceUtils.getSurfaceSize(SurfaceUtils.java:68)
                      at android.hardware.camera2.params.OutputConfiguration.&lt;init&gt;(OutputConfiguration.java:97) 
                      at android.hardware.camera2.params.OutputConfiguration.&lt;init&gt;(OutputConfiguration.java:71) 
                      at android.hardware.camera2.impl.CameraDeviceImpl.createCaptureSession(CameraDeviceImpl.java:474) 
                      at com.example.universitywork.canyouwork.MainActivity$1.onOpened(MainActivity.java:40) 
                      at android.hardware.camera2.impl.CameraDeviceImpl$1.run(CameraDeviceImpl.java:134) 
                      at android.os.Handler.handleCallback(Handler.java:739) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      at android.os.Looper.loop(Looper.java:148) 
                      at android.app.ActivityThread.main(ActivityThread.java:5417) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
</code></pre>

<p>I think it has to do with the size or the underlying surface of the SurfaceView. Maybe im not using it correctly but the Android documentation says it can be made with SurfaceView. </p>