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

Conversation

Gioxxy
Copy link

@Gioxxy Gioxxy commented Jul 13, 2020

Description

Fix "android.view.Surface android.media.ImageReader.getSurface() on a null object reference"
which causes the app to crash on Android devices immediately after requesting permission to use the camera if the CameraPreview is not visible (example if it is on the second page of a PageView)

Related Issues

flutter/flutter#19595

Checklist

Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes ([x]). This will ensure a smooth and quick review process.

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • My PR includes unit or integration tests for all changed/updated/fixed behaviors (See Contributor Guide).
  • All existing and new tests are passing.
  • I updated/added relevant documentation (doc comments with ///).
  • The analyzer (flutter analyze) does not report any problems on my PR.
  • I read and followed the Flutter Style Guide.
  • The title of the PR starts with the name of the plugin surrounded by square brackets, e.g. [shared_preferences]
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy.
  • I updated CHANGELOG.md to add a description of the change.
  • I signed the CLA.
  • I am willing to follow-up on review comments in a timely manner.

Breaking Change

Does your PR require plugin users to manually update their apps to accommodate your change?

  • Yes, this is a breaking change (please indicate a breaking change in CHANGELOG.md and increment major revision).
  • No, this is not a breaking change.

… 'android.view.Surface android.media.ImageReader.getSurface()' on a null object reference"
@AlexV525
Copy link
Member

Hi! I found that your PR includes unformatted code, can you fix them? 😄

/cc @mklim

@Gioxxy
Copy link
Author

Gioxxy commented Jul 23, 2020

Hi! I found that your PR includes unformatted code, can you fix them? 😄

/cc @mklim

Yes, i will fix it soon

@AlexV525
Copy link
Member

It seems Michael is AFK for a while, let me see if I can find someone for this.

@@ -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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants