NPE caused by native ads after switching between pages and sending the app to background
See original GitHub issuePlugin Version
google_mobile_ads 1.0.1
Steps to Reproduce
- Implement native ads following the Kotlin example from codelabs
- Open app, check that the native ad is loaded and visible
- Open a drawer in the app or open a different page in the app so the native ad is covered
- Send the app to background
Expected results: App doesn’t crash
Actual results: App crashes
Logs
E/AndroidRuntime(30623): FATAL EXCEPTION: main
E/AndroidRuntime(30623): Process: com.my.app, PID: 30623
E/AndroidRuntime(30623): java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.dispatchWindowVisibilityChanged(int)' on a null object reference
E/AndroidRuntime(30623): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1290)
E/AndroidRuntime(30623): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1290)
E/AndroidRuntime(30623): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1290)
E/AndroidRuntime(30623): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1290)
E/AndroidRuntime(30623): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1290)
E/AndroidRuntime(30623): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1813)
E/AndroidRuntime(30623): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1515)
E/AndroidRuntime(30623): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7091)
E/AndroidRuntime(30623): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:927)
E/AndroidRuntime(30623): at android.view.Choreographer.doCallbacks(Choreographer.java:702)
E/AndroidRuntime(30623): at android.view.Choreographer.doFrame(Choreographer.java:638)
E/AndroidRuntime(30623): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:913)
E/AndroidRuntime(30623): at android.os.Handler.handleCallback(Handler.java:751)
E/AndroidRuntime(30623): at android.os.Handler.dispatchMessage(Handler.java:95)
E/AndroidRuntime(30623): at android.os.Looper.loop(Looper.java:154)
E/AndroidRuntime(30623): at android.app.ActivityThread.main(ActivityThread.java:6682)
E/AndroidRuntime(30623): at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(30623): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
E/AndroidRuntime(30623): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
flutter doctor -v
[✓] Flutter (Channel stable, 2.8.1, on macOS 11.3.1 20E241 darwin-x64, locale en-US)
• Flutter version 2.8.1 at /Users/me/Downloads/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 77d935af4d (2 weeks ago), 2021-12-16 08:37:33 -0800
• Engine revision 890a5fca2e
• Dart version 2.15.1
[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
• Android SDK at /Users/me/Library/Android/sdk
• Platform android-31, build-tools 31.0.0
• ANDROID_HOME =
/usr/local/opt/python@3.8/bin:/Users/me/.rvm/gems/ruby-2.7.0/bin:/Users/me/.rvm/gems/ruby-2.7.0@global/bin:/Users/me/.rvm/rubies/ruby-2.7.0/bin:/usr/local/bin:/usr/local
/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin:/Users/me/n/bin:/Users/me/.rvm/bin:/Users/me/Library/Android/sdk/tools:/Users/me/Library/Android/sdk:/Use
rs/me/Library/Android/sdk/platform-tools:/Users/me/Library/Android/sdk/tools
• ANDROID_SDK_ROOT = /Users/me/Library/Android/sdk/
• Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7281165)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 13.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• CocoaPods version 1.11.2
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 2020.3)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7281165)
[✓] IntelliJ IDEA Community Edition (version 2020.1.4)
• IntelliJ at /Applications/IntelliJ IDEA CE.app
• Flutter plugin version 56.0.1
• Dart plugin version 201.9380
[✓] VS Code (version 1.50.1)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.17.0
[✓] Connected device (3 available)
• SM G920F (mobile) • 03157df394036a23 • android-arm64 • Android 7.0 (API 24)
• No issues found!
The crash occurs on Android, reproduced it on devices from Android 7 to Android 11 and there are several crashes in production as well. Tried with older version of Flutter, same crash. Also older versions of the plugin provoke the same issue.
Is the codelabs example missing some additional setup or is this plugin just unusable in the current state?
Issue Analytics
- State:
- Created 2 years ago
- Comments:6
Top Results From Across the Web
About native ads - Display & Video 360 Help - Google Support
Native ads match the look and feel of a site or app and provide a better user experience for visitors. Native ads are...
Read more >android - How to change the background of activity in fragment?
I want to change the background of activity in fragment. When I click the button in the Fragment , it choose the picture....
Read more >Native Advertising: A Guide for Businesses
When a native ad appears on the main page of a publisher site or is republished in other media, it commonly consists of...
Read more >Android Changelog - Airship Docs
This change should reduce the chances of showing out of data or cancelled in-app automations, scenes, or surveys when background refresh is disabled....
Read more >Firebase Android SDK Release Notes
Learn how to install these SDKs in your app: Add Firebase to your Android ... Fixed a race condition that was caused when...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi @maheshmnj , thank you for the quick reply.
After checking the code in detail I found what caused the problem in my case.
To try to improve native ad performance I called
PlatformViewsService.synchronizeToNativeViewHierarchy(false)
in Flutter main. After removing this it doesn’t crashes anymore.Performance is still terrible because of the ad also in the example app, but that’s another topic. At least the crash is gone.
Thank you!
Thanks for the info @kreativityapps, Glad you solved it.
I agree there are performance issues and they are being tracked in the repo. Please follow up there for further updates.
Closing as solved.