4

I have an activity where I initialize barcode detection. Then I go to another activity and when I press the back button it fires a loop of this exception:

com.example.myapp E/CameraSource: Exception thrown from receiver.
                                  java.lang.NullPointerException
                      at com.google.android.gms.vision.CameraSource$zzb.run(Unknown Source)
                      at java.lang.Thread.run(Thread.java:841)

If I insist and go again to the other activity it fires another exception:

com.example.myapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      Process: com.example.myapp, PID: 22942
                                                                      java.lang.NullPointerException
                                                                          at com.google.android.gms.vision.CameraSource$zzb.release(Unknown Source)
                                                                          at com.google.android.gms.vision.CameraSource.release(Unknown Source)
                                                                          at com.example.myapp.CameraActivity$3.surfaceDestroyed(CameraActivity.java:184)
                                                                          at android.view.SurfaceView.updateWindow(SurfaceView.java:586)
                                                                          at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:240)
                                                                          at android.view.View.dispatchWindowVisibilityChanged(View.java:8495)
                                                                          at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1087)
                                                                          at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1087)
                                                                          at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1087)
                                                                          at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1087)
                                                                          at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1087)
                                                                          at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1087)
                                                                          at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1537)
                                                                          at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
                                                                          at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6602)
                                                                          at android.view.Choreographer$CallbackRecord.run(Choreographer.java:805)
                                                                          at android.view.Choreographer.doCallbacks(Choreographer.java:605)
                                                                          at android.view.Choreographer.doFrame(Choreographer.java:575)
                                                                          at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:791)
                                                                          at android.os.Handler.handleCallback(Handler.java:733)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                          at android.os.Looper.loop(Looper.java:136)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5476)
                                                                          at java.lang.reflect.Method.invokeNative(Native Method)
                                                                          at java.lang.reflect.Method.invoke(Method.java:515)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
                                                                          at dalvik.system.NativeStart.main(Native Method)

CameraSource is initialized like this in onCreate:

    final CameraSource cameraSource = new CameraSource.Builder(this, barcodeDetector)
            .setRequestedFps(15f)
            .setAutoFocusEnabled(true)
            .build();
    mCameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            try {
                if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
                    return;
                cameraSource.start(holder);
            } catch (IOException e) {
                Log.e(getClass().getName(), e.getMessage());
            }
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            cameraSource.release();
        }
    });
Fabricio
  • 7,705
  • 9
  • 52
  • 87
  • Activities Pause and Resume if you jump to another activity and then you go back to them. Take a look here on how to release the camera on Pause and initialize it again on Resume. It may do the trick but not 100% in your case it may be a different issue -- https://developer.android.com/training/basics/activity-lifecycle/pausing.html – Tasos Aug 16 '16 at 19:32
  • 1
    I moved the inicialization code from `onCreate` to `onResume` and protected the camera release as your link said and now it's fine. Thank you. – Fabricio Aug 17 '16 at 11:41
  • 1
    meant to say (not 100% sure if that would help) but i guess it did :) – Tasos Aug 17 '16 at 12:42

1 Answers1

3

I moved the camera initialization code from onCreate to onResume and protected the camera release like this:

mCameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
        ...
        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            if (mCameraSource != null) {
                mCameraSource.release();
                mCameraSource = null;
            }
        }
    });

Now it's working.

Fabricio
  • 7,705
  • 9
  • 52
  • 87