Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crashing while showing banner ad #341

Open
StevieApp opened this issue Jan 30, 2025 · 0 comments
Open

Crashing while showing banner ad #341

StevieApp opened this issue Jan 30, 2025 · 0 comments

Comments

@StevieApp
Copy link

StevieApp commented Jan 30, 2025

Describe the bug
There is an app crash issue while running ads

Process: com.StevieApp.TeachieApp
PID: 21752
UID: 10263
Frozen: false
Flags: 0x30c8be44
Package: com.StevieApp.TeachieApp v1 (1.0)
Foreground: Yes
Process-Runtime: 12052
Build: google/redfin_64/redfin:13/TP1A.220624.014/8819323:userdebug/dev-keys
Loading-Progress: 1.0
Dropped-Count: 0

Issue

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.ads.AdView.loadAd(com.google.android.gms.ads.AdRequest)' on a null object reference
	at com.getcapacitor.community.admob.banner.BannerExecutor.lambda$updateExistingAdView$3(BannerExecutor.java:218)
	at com.getcapacitor.community.admob.banner.BannerExecutor.$r8$lambda$rG8pevl4TYjNjEUjyIXkgzJ2UsA(Unknown Source:0)
	at com.getcapacitor.community.admob.banner.BannerExecutor$$ExternalSyntheticLambda1.run(D8$$SyntheticClass:0)
	at android.os.Handler.handleCallback(Handler.java:942)
	at android.os.Handler.dispatchMessage(Handler.java:99)
	at android.os.Looper.loopOnce(Looper.java:201)
	at android.os.Looper.loop(Looper.java:288)
	at android.app.ActivityThread.main(ActivityThread.java:7898)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)

To Reproduce
Steps to reproduce the behavior:

  1. Use the code initialization provided in the first page
  2. There is no js error just an issue that I thought wasn't persistent until I tried publishing the app

Expected behavior
Ads show with no problems

Screenshots
If applicable, add screenshots to help explain your problem.

Image

Smartphone (please complete the following information):

  • Device: google Pixel 5 64-bit only 1080x2340 en_US
  • OS: Android 13
  • Browserapk
  • Version SDK 33

Additional context
just a simple code implementation

Code used

ngOnInit() {  
    if(this.isMobile){
      this.initializeAdMob();
    }  
  }

async initializeAdMob(): Promise<void> {
    await AdMob.initialize();
  
    const [trackingInfo, consentInfo] = await Promise.all([
      AdMob.trackingAuthorizationStatus(),
      AdMob.requestConsentInfo(),
    ]);

    if (trackingInfo.status === 'notDetermined') {
      await AdMob.requestTrackingAuthorization();
    }
  
    const authorizationStatus = await AdMob.trackingAuthorizationStatus();
    if (
      authorizationStatus.status === 'authorized' 
      && consentInfo.isConsentFormAvailable 
      && consentInfo.status === AdmobConsentStatus.REQUIRED
    ) {
      await AdMob.showConsentForm();
    }
    console.log('Consent Info:', consentInfo);
    console.log('Tracking Authorization:', authorizationStatus);

    await this.showBanner(null)
  }


  async listeners(): Promise<void> {
    if (!this.listenersRegistered) {
      var padder: any = document.getElementById('tabber')
      // Add listeners only once
      AdMob.addListener(BannerAdPluginEvents.Loaded, () => {
        console.log('Banner ad loaded');
      });

      AdMob.addListener(BannerAdPluginEvents.SizeChanged, (size: AdMobBannerSize) => {
        const bannerHeight = size.height;
        padder.style.paddingBottom  = `${bannerHeight}px`;
        console.log('Banner size changed:', size);
      });

      AdMob.addListener(BannerAdPluginEvents.FailedToLoad, (error) => {
        padder.style.paddingBottom = '0px';
        console.error('Banner failed to load:', error);
      });

      this.listenersRegistered = true; // Mark listeners as registered
    }
  }

  async showBanner(event: any): Promise<void> {
    // console.log('changedddddddddddddddddddddd')
    // console.log(event)
    if(event && event.tab == 'profile'){
      this.runAction()
    }
    try {
      await AdMob.hideBanner();
      await AdMob.removeBanner();
    } catch (error) {
      console.log('Error hiding/removing banner:', error);
    }
    this.listeners()
    const options: BannerAdOptions = {
     // I have my ad id here
      adId: 'ca-app-pub-xxxxxxxxxxxxxxxxxxxxxxx',
      adSize: BannerAdSize.BANNER,
      position: BannerAdPosition.BOTTOM_CENTER,
      margin: 0,
      // isTesting: true, // Set true for testing
    };

    try {
      // Show banner
      await AdMob.showBanner(options);
    } catch (error) {
      console.error('Error showing banner:', error);
    }
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant