title: Android camera permission
body: <p>I have this code which i make using official developer android sites. I want to create camera preview, but first ask for permissions. When i start the app they crash, but before app exit, they ask me for permissions which i grant, and then app crash. After i enter app again it work just fine, but i want to get rid of that crash. Anyone know a solution?</p>
  
  <p><strong>CameraActivity.java</strong></p>
  
  <pre><code>public class CameraActivity extends AppCompatActivity {
  
      private Camera mCamera;
      private CameraPreview mPreview;
      private static final int PERMISSION_CAMERA = 1;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_camera);
  
          if (ContextCompat.checkSelfPermission(this,
                  Manifest.permission.CAMERA)
                  != PackageManager.PERMISSION_GRANTED) {
  
              // Should we show an explanation?
              if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                      Manifest.permission.CAMERA)) {
  
                  // Show an explanation to the user *asynchronously* -- don't block
                  // this thread waiting for the user's response! After the user
                  // sees the explanation, try again to request the permission.
  
              } else {
  
                  // No explanation needed, we can request the permission.
  
                  ActivityCompat.requestPermissions(this,
                          new String[]{Manifest.permission.CAMERA},
                          PERMISSION_CAMERA);
  
                  // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
                  // app-defined int constant. The callback method gets the
                  // result of the request.
              }
          }
  
          // Create an instance of Camera
          mCamera = getCameraInstance();
  
          // Create our Preview view and set it as the content of our activity.
          mPreview = new CameraPreview(this, mCamera);
          FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
          preview.addView(mPreview);
      }
  
      @Override
      public void onRequestPermissionsResult(int requestCode,
                                             String permissions[], int[] grantResults) {
          switch (requestCode) {
              case PERMISSION_CAMERA: {
                  // If request is cancelled, the result arrays are empty.
                  if (grantResults.length &gt; 0
                          &amp;&amp; grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  
                      // permission was granted, yay! Do the
                      // contacts-related task you need to do.
  
                  } else {
  
                      // permission denied, boo! Disable the
                      // functionality that depends on this permission.
                  }
                  return;
              }
  
              // other 'case' lines to check for other
              // permissions this app might request.
          }
      }
  
      @Override
      public void onPause() {
          super.onPause();
          // Stop camera access
          releaseCamera();
      }
  
      /** A safe way to get an instance of the Camera object. */
      public static Camera getCameraInstance(){
          Camera c = null;
          try {
              c = Camera.open(); // attempt to get a Camera instance
          }
          catch (Exception e){
              // Camera is not available (in use or does not exist)
          }
          return c; // returns null if camera is unavailable
      }
  
      private void releaseCamera(){
          if (mCamera != null){
              mCamera.release();        // release the camera for other applications
              mCamera = null;
          }
      }
  }
  </code></pre>
  
  <p><strong>Logcat</strong></p>
  
  <pre><code>FATAL EXCEPTION: main
  Process: com.example.ivan.cameratest1, PID: 7690
  java.lang.NullPointerException: Attempt to invoke virtual method 'void android.hardware.Camera.setPreviewDisplay(android.view.SurfaceHolder)' on a null object reference
  at com.example.ivan.cameratest1.CameraPreview.surfaceCreated(CameraPreview.java:36)
  at android.view.SurfaceView.updateWindow(SurfaceView.java:656)
  at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:172)
  at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:1013)
  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2542)
  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1537)
  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7183)
  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:959)
  at android.view.Choreographer.doCallbacks(Choreographer.java:734)
  at android.view.Choreographer.doFrame(Choreographer.java:670)
  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:945)
  at android.os.Handler.handleCallback(Handler.java:751)
  at android.os.Handler.dispatchMessage(Handler.java:95)
  at android.os.Looper.loop(Looper.java:154)
  at android.app.ActivityThread.main(ActivityThread.java:6776)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
  </code></pre>