Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

[camera] Fix 'getSurface() on a null object reference' Android crash #2871

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,8 @@ public void resumeVideoRecording(@NonNull final Result result) {
}

public void startPreview() throws CameraAccessException {
if (pictureImageReader == null || pictureImageReader.getSurface() == null) return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drive-by code review....

Under what circumstances is it null? It looks like it gets set in open() and null'd out in close(). Is the error you're seeing that the surface is null, and if so, how/when does the surface get populated?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this error: "android.view.Surface android.media.ImageReader.getSurface() on a null object reference" if the surface is not visible immediately after accepting the camera permissions, and the app crashes. If I reopen the app, I no longer see the error. I think that an extra check for null safety is not bad if it saves from crashes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Gioxxy could you possibly update your pubspec for your app to point to a locally modified camera plugin with heavy instrumentation (Android logs) added, so you can look at the logs and see what order stuff is happening in?

dependency_overrides:
  camera:
    git:
      url: git://github.com/Gioxxy/plugins.git
      ref: fix_android_null_surface
      path: packages/camera

I'd like to understand how it comes to be that pictureImageReader or pictureImageReader.getSurface() gets nulled out - maybe there's a better solution than a simple null check here.


createCaptureSession(CameraDevice.TEMPLATE_PREVIEW, pictureImageReader.getSurface());
}

Expand Down