title: How to display camera in fragment?
body: <p>I'm trying to access the camera within the fragment in android studio.
  these are the codes I wrote
  My Fragment Codes:</p>
  
  <pre><code>    package com.example.proje.ui.camera;
  
  import android.hardware.Camera;
  import android.os.Bundle;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.FrameLayout;
  import android.widget.TextView;
  
  import androidx.annotation.NonNull;
  import androidx.annotation.Nullable;
  import androidx.fragment.app.Fragment;
  import androidx.lifecycle.Observer;
  import androidx.lifecycle.ViewModelProviders;
  import com.example.proje.ui.camera.CameraViewModel;
  import com.example.proje.R;
  
  public class CameraFragment extends Fragment {
  
  
      Camera camera;
      FrameLayout frameLayout;
      ShowCamera showCamera;
  
      @Override
      public View onCreateView(@NonNull LayoutInflater inflater,
                               ViewGroup container, Bundle savedInstanceState) {
  
          View root = inflater.inflate(R.layout.fragment_camera, container, false);
  
          return root;
  
  
     }
      @Override
      public void onCreate(@Nullable Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
  
  
          frameLayout = (FrameLayout)getActivity().findViewById(R.id.frameLayout);
  
          camera = Camera.open();
  
          showCamera = new ShowCamera(getContext(), camera);
          frameLayout.addView(showCamera);
      }
  }
  </code></pre>
  
  <p>ShowCamera code:</p>
  
  <pre><code>    package com.example.proje.ui.camera;
  
      import android.content.Context;
  import android.content.res.Configuration;
  import android.hardware.Camera;
  import android.view.SurfaceHolder;
  import android.view.SurfaceView;
  
  import java.io.IOException;
  
  public class ShowCamera extends SurfaceView implements SurfaceHolder.Callback
  {
  
      Camera camera;
      SurfaceHolder holder;
      public ShowCamera(Context context, Camera camera) {
          super(context);
          this.camera = camera;
          holder = getHolder();
          holder.addCallback(this);
      }
  
      @Override
      public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
  
      }
  
      @Override
      public void surfaceDestroyed(SurfaceHolder holder) {
  
      }
  
      @Override
      public void surfaceCreated(SurfaceHolder holder) {
          Camera.Parameters params = camera.getParameters();
  
          if(this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE)
          {
              params.set("orientation", "portrait");
              camera.setDisplayOrientation(90);
              params.setRotation(90);
          }
          else
          {
              params.set("orientation", "landscape");
              camera.setDisplayOrientation(0);
              params.setRotation(0);
          }
  
          camera.setParameters(params);
          try
          {
              camera.setPreviewDisplay(holder);
  
              camera.startPreview();
          } catch (IOException e)
          {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>I gave the necessary permissions from the android manifest file.
  compiles seamlessly in android studio but when I run it on the phone I get the application stopped error. </p>
  
  <p>How can I solve this problem?</p>