natario1 / CameraView

📸 A well documented, high-level Android interface that makes capturing pictures and videos easy, addressing all of the common issues and needs. Real-time filters, gestures, watermarks, frame processing, RAW, output of any size.
https://natario1.github.io/CameraView
Other
4.94k stars 936 forks source link

NullpointerException on onStartBind() #1171

Open SweetD3v opened 2 years ago

SweetD3v commented 2 years ago

Hi tech folks. I am getting this bug again and again in some devices. I tried many times to solve that, but it's still happening for some reason. Anyone having this issue? then please help me solve it.

To Reproduce

It's not reproduceable sadly, I am getting it from the play store app although I didn't apply any proguard.

XML layout

<com.otaliastudios.cameraview.CameraView
                android:id="@+id/cameraView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:animateLayoutChanges="true"
                android:keepScreenOn="true"
                app:cameraAudio="on"
                app:cameraAutoFocusMarker="@string/cameraview_default_autofocus_marker"
                app:cameraEngine="camera2"
                app:cameraExperimental="true"
                app:cameraFacing="back"
                app:cameraGestureLongTap="none"
                app:cameraGesturePinch="zoom"
                app:cameraGestureScrollVertical="exposureCorrection"
                app:cameraHdr="on"
                app:cameraMode="picture"
                app:cameraPictureMetering="false"
                app:cameraPlaySounds="false"
                app:cameraPreview="glSurface"
                app:cameraPreviewFrameRate="30"
                app:cameraPreviewFrameRateExact="true"
                app:cameraRequestPermissions="false"/>

Logs

java.lang.NullPointerException: at com.otaliastudios.cameraview.engine.Camera2Engine.onStartBind (Camera2Engine.java) at com.otaliastudios.cameraview.engine.CameraEngine$2.call (CameraEngine.java:22) at com.otaliastudios.cameraview.engine.CameraEngine$2.call (CameraEngine.java) at com.otaliastudios.cameraview.engine.orchestrator.CameraStateOrchestrator$1.call (CameraStateOrchestrator.java) at com.otaliastudios.cameraview.engine.orchestrator.CameraStateOrchestrator$1.call (CameraStateOrchestrator.java) at com.otaliastudios.cameraview.engine.orchestrator.CameraOrchestrator$2.run (CameraOrchestrator.java:4) at com.otaliastudios.cameraview.internal.WorkerHandler.run (WorkerHandler.java:10) at com.otaliastudios.cameraview.engine.orchestrator.CameraOrchestrator.execute (CameraOrchestrator.java:13) at com.otaliastudios.cameraview.engine.orchestrator.CameraOrchestrator.lambda$sync$0 (CameraOrchestrator.java:46) at com.otaliastudios.cameraview.engine.orchestrator.CameraOrchestrator.$r8$lambda$HmE6GLzVomrx4z2yFPqPM3-OldU (CameraOrchestrator.java) at com.otaliastudios.cameraview.engine.orchestrator.CameraOrchestrator$$InternalSyntheticLambda$0$8b677079dbbaae903a74c190d1ab73dae346936de4769c0e3a915f12c512d38e$0.run (CameraOrchestrator.java:2) at android.os.Handler.handleCallback (Handler.java:938) at android.os.Handler.dispatchMessage (Handler.java:99) at android.os.Looper.loop (Looper.java:236) at android.os.HandlerThread.run (HandlerThread.java:67)

razlesh1 commented 2 years ago

Having a similar issue. Anyone solved it?

SweetD3v commented 2 years ago

I reproduced it while testing.

I used an interface which will show user a dialog with text that something is wrong with camera and press ok to restart. Then it'll restart the app on OK button press.

razlesh1 commented 2 years ago

Hi, thank you for answering. I didn't quite understand what "Put open app ads." means? Also, where did you call the callback for when something is wrong with the camera?

SweetD3v commented 2 years ago

Implement admob's "open app" ads to reproduce this bug easily. Where did I put the interface?

In the catch statement inside onStartBind()

try {
            mCamera.createCaptureSession(outputSurfaces, new CameraCaptureSession.StateCallback() {
                @Override
                public void onConfigured(@NonNull CameraCaptureSession session) {
                    mSession = session;
                    task.trySetResult(null);
                }

                @Override
                public void onConfigureFailed(@NonNull CameraCaptureSession session) {
                    Throwable cause = new RuntimeException(session.toString());
                    if (!task.getTask().isComplete()) {
                        task.trySetException(new CameraException(cause,
                                CameraException.REASON_FAILED_TO_START_PREVIEW));
                    } else {
                        throw new CameraException(CameraException.REASON_DISCONNECTED);
                    }
                }

                @Override
                public void onReady(@NonNull CameraCaptureSession session) {
                    super.onReady(session);
                }
            }, null);
        } catch (CameraAccessException e) {
//            throw createCameraException(e);
//Here I put that
            new Handler(Looper.getMainLooper())
                    .post(() -> {
                        if (CameraView.restartAppListener != null)
                            CameraView.restartAppListener.onRestartApp();
                        else Toast.makeText(getCallback().getContext(), "Camera error", Toast.LENGTH_SHORT).show();
                    });
        }

In your camera activity,

@Override
public void onRestartApp() {
    restartCameraDialog();
}
SweetD3v commented 2 years ago

I'm getting too many crashes in this onStartBind() method. Anyone found any solution? @natario1 ?