Home > Back-end >  Flutter app with AdMob crash after pushing to another page and moving the app to the background
Flutter app with AdMob crash after pushing to another page and moving the app to the background

Time:02-12

Now I have a two-page app with the first page to show an adMob banner.

When I move the app to the background, everything is good.

If I navigate to the second page, one logging message is shown.

E/Surface (12399): getSlotFromBufferLocked: unknown buffer: 0xbc288b10

Then I move the app to the background again, while in the second page, the app crashes with the following log.

E/BufferQueueProducer(12399): [ImageReader-1080x1794f1m3-12399-1](id:306f00000001,api:1,p:12399,c:12399) cancelBuffer: BufferQueue has been abandoned
D/AndroidRuntime(12399): Shutting down VM
E/AndroidRuntime(12399): FATAL EXCEPTION: main
E/AndroidRuntime(12399): Process: com.example.testapp, PID: 12399
E/AndroidRuntime(12399): java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.dispatchWindowVisibilityChanged(int)' on a null object reference
4
E/AndroidRuntime(12399):    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1614)
E/AndroidRuntime(12399):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2433)
E/AndroidRuntime(12399):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1952)
E/AndroidRuntime(12399):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8171)
E/AndroidRuntime(12399):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:972)
E/AndroidRuntime(12399):    at android.view.Choreographer.doCallbacks(Choreographer.java:796)
E/AndroidRuntime(12399):    at android.view.Choreographer.doFrame(Choreographer.java:731)
E/AndroidRuntime(12399):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:957)
E/AndroidRuntime(12399):    at android.os.Handler.handleCallback(Handler.java:938)
E/AndroidRuntime(12399):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(12399):    at android.os.Looper.loop(Looper.java:223)
E/AndroidRuntime(12399):    at android.app.ActivityThread.main(ActivityThread.java:7656)
E/AndroidRuntime(12399):    at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(12399):    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
E/AndroidRuntime(12399):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
I/Process (12399): Sending signal. PID: 12399 SIG: 9
Lost connection to device.

The first page source

import 'package:flutter/material.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'second_page_screen.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  MobileAds.instance.initialize();
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: const Text("Ads"),
          ),
          body: MainBody()),
    );
  }
}

class MainBody extends StatefulWidget {
  MainBody({Key? key}) : super(key: key);

  @override
  State<MainBody> createState() => _MainBodyState();
}

class _MainBodyState extends State<MainBody> {
  late BannerAd myBanner;
  late BannerAdListener listener;
  late AdWidget adWidget;
  late Container adContainer;

  @override
  void initState() {
    super.initState();

    listener = BannerAdListener(
      onAdLoaded: (Ad ad) => print('Ad loaded.'),
      onAdFailedToLoad: (Ad ad, LoadAdError error) {
        ad.dispose();
        print('Ad failed to load: $error');
      },
      onAdOpened: (Ad ad) => print('Ad opened.'),
      onAdClosed: (Ad ad) => print('Ad closed.'),
      onAdImpression: (Ad ad) => print('Ad impression.'),
    );

    myBanner = BannerAd(
      // final BannerAd myBanner = BannerAd(
      adUnitId: 'ca-app-pub-3940256099942544/6300978111',
      size: AdSize.largeBanner,
      request: const AdRequest(
        keywords: <String>['foo', 'bar'],
        contentUrl: 'http://example.com',
        nonPersonalizedAds: true,
      ),
      listener: listener,
    );

    myBanner.load();
    adWidget = AdWidget(ad: myBanner);

    adContainer = Container(
      alignment: Alignment.center,
      child: adWidget,
      width: myBanner.size.width.toDouble(),
      height: myBanner.size.height.toDouble(),
    );
  }

  @override
  void dispose() {
    super.dispose();
    myBanner.dispose();
  }

  @override
  Widget build(BuildContext context) {
    void _pushPage() {
      Navigator.push(
        context,
        MaterialPageRoute(
          builder: (context) => const SecondPageScreen(),
        ),
      );
    }

    return Column(
      children: [
        Container(
          decoration:
              BoxDecoration(border: Border.all(color: Colors.black, width: 2)),
          child: adContainer,
        ),
        TextButton(onPressed: _pushPage, child: Text('Next Page'))
      ],
    );
  }
}

[Here is the screenshot of the first page, just for quick reference][1] [1]: https://i.stack.imgur.com/Mdi67.png

The second page source

import 'package:flutter/material.dart';

class SecondPageScreen extends StatelessWidget {
  const SecondPageScreen({ Key? key }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('second page')),
      body: Container(

        child: Text('hi')
      ),
    );
  }
}

What I have done:

I tried following the AdMob documentation step by step: https://developers.google.com/admob/flutter/quick-start

And I used the designated test banner ad as shown on the following page: https://developers.google.com/admob/android/test-ads

I registered the app id in adMob

Quite sure that I made the changes in gradle/androidmanifest/pubspec files correctly.

but in vain...

CodePudding user response:

I have this same issue in my application, although I do not fully crash. I do get getSlotFromBufferLocked every time I push to a page that does not have the ad widget on it. If I solve this, I will post back here with a solution.

CodePudding user response:

My friend searched me a page which produced the same exception

https://github.com/flutter/flutter/issues/96875

the problem is solved by downgrading flutter to 2.8.1

  • Related