Skip to content Skip to sidebar Skip to footer

How To Solve "android.view.surfaceview.getholder()' On A Null Object Reference"?

public class CameraFragment extends Fragment implements SurfaceHolder.Callback,View.OnClickListener { // Defined All Field private SurfaceView surfaceView; private Surf

Solution 1:

Whenever you found same as my problem, then do the following. Just replace your OnCreateView() code into onViewCreated(), because OnCreateView() method is only useful for inflating a View.

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_camera, container, false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    // Camera surface view created
    mCameraId = Camera.CameraInfo.CAMERA_FACING_BACK;
    btnFlash = (Button) view.findViewById(R.id.btnFlash);
    btnFrontCamera = (Button) view.findViewById(R.id.btnFrontCamera);
    btnCapture = (Button) view.findViewById(R.id.btnCapture);

    surfaceView = (SurfaceView) view.findViewById(R.id.surfaceView);
    surfaceHolder = surfaceView.getHolder();
    surfaceHolder.addCallback(this);

    ImgThumbnail = (ImageView) view.findViewById(R.id.ImgThumbnail);

    btnFrontCamera.setOnClickListener(this);
    btnCapture.setOnClickListener(this);
    btnFlash.setOnClickListener(this);
    getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    if (Camera.getNumberOfCameras() > 1) {
        btnFrontCamera.setVisibility(View.VISIBLE);
    }
    if (!getActivity().getBaseContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
        btnFlash.setVisibility(View.GONE);
    }
}

Post a Comment for "How To Solve "android.view.surfaceview.getholder()' On A Null Object Reference"?"