google / cameraview

[DEPRECATED] Easily integrate Camera features into your Android app
Apache License 2.0
4.74k stars 1.03k forks source link

NULL reference exception on takePicture() #29

Open remq opened 8 years ago

remq commented 8 years ago

I only got this error on my Huawei Ascend P7 device and not on a Samsung Galaxy S5. When taking a picture, it crashes.

mTakePicture.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (mCameraView.isCameraOpened()) {
            mCameraView.takePicture();
        }
    }
});

mCameraView.addCallback(new CameraView.Callback() {
    @Override
    public void onPictureTaken(CameraView cameraView, byte[] data) {
        super.onPictureTaken(cameraView, data);
        mListener.onPictureTaken(data); // Custom method to process the data
        mCameraView.stop();
        getActivity().getSupportFragmentManager().popBackStack();
    }
});

When deleting the following line it works, however I need to close the camera fragment. getActivity().getSupportFragmentManager().popBackStack();

This is the error:

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.hardware.camera2.CameraCaptureSession.capture(android.hardware.camera2.CaptureRequest, android.hardware.camera2.CameraCaptureSession$CaptureCallback, android.os.Handler)' on a null object reference
    at com.google.android.cameraview.Camera2.unlockFocus(Camera2.java:625)
    at com.google.android.cameraview.Camera2.access$900(Camera2.java:45)
    at com.google.android.cameraview.Camera2$6.onCaptureCompleted(Camera2.java:609)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at android.hardware.camera2.dispatch.InvokeDispatcher.dispatch(InvokeDispatcher.java:39)
    at android.hardware.camera2.dispatch.HandlerDispatcher$1.run(HandlerDispatcher.java:65)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5569)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:931)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:726)

My guess is that I need to listen to another callback before killing the camera, but I couldn't find one.

remq commented 8 years ago

I've fixed it for myself by adding a confirm-picture screen. This way the camera has enough time to close itself and it's a good practice anyway. However, this issue might be worth looking into.

PavelSynek commented 7 years ago

I solved the problem by calling camera.stop() instead of navigating away and overriding onCameraClosed in CameraView.Callback. I wait until the camera is closed and then perform the navigation.

PeterJiung commented 7 years ago

i have the same bug

jinqian commented 7 years ago

FYI, I have the same bug on Samsung Galaxy S7, API 23

JMoicano commented 6 years ago

@PavelSynek can you give a sample of how your sollution works?

PavelSynek commented 6 years ago

Sure! I don't think I can find the exact code, but this pseudocode should help too.

private boolean navigateAfterCameraStop = false;

cameraView.addCallback(new CameraView.Callback() {
    @Override
    public void onPictureTaken(CameraView cameraView, byte[] data) {
         //
    }

    @Override
    public void onCameraClosed() {
        if (navigateAfterCameraStop) {
            navigateAfterCameraStop = false;
            navigateForReal();
        }
    }
});

public void onNavigate() {
    // Do not navigate, but set the flag, stop camera, wait for it to close and then navigate
    navigateAfterCameraStop = true;
    cameraView.stop();
}
M0N3 commented 5 years ago

Just added

catch (NullPointerException n) {
            n.printStackTrace();
        }

to method unlockFocus() in Camera2.java class Dirty trick, but works.

siokagami commented 5 years ago

Just added

catch (NullPointerException n) {
            n.printStackTrace();
        }

to method unlockFocus() in Camera2.java class Dirty trick, but works.

Really work for me!!!!

yink-liu commented 5 years ago

Just added

catch (NullPointerException n) {
            n.printStackTrace();
        }

to method unlockFocus() in Camera2.java class Dirty trick, but works.

Just added

catch (NullPointerException n) {
            n.printStackTrace();
        }

to method unlockFocus() in Camera2.java class Dirty trick, but works.

Really work for me!!!!

+1

andzejsw commented 4 years ago

Same error on Xiaomi: Redmi Note 7.

pankaj529 commented 4 years ago

@andzejsw got any solution??