Xamarin.Android : Admob v20 IntertitialAds problem

Abdellah Amri 1 Reputation point
2021-09-05T17:32:14.73+00:00

Can you please explain or post an example of using admob v20.. intertitial or rewareded ads in xamarin android app
i tried so many examples in the web i still can't show interitial ads ... banner ads are working fine
i'm getting this error

System.NullReferenceException: 'Object reference not set to an instance of an object.'

i've tried this hack class

https://gist.github.com/dtaylorus/63fef408cec34999a1e566bd5fac27e5

and some examples from this link but with no vain

https://github.com/xamarin/GooglePlayServicesComponents/issues/425

Thank you

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,312 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 27,901 Reputation points Microsoft Vendor
    2021-09-06T08:27:22.21+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    I'm afraid it is still a not fix issue of Xamarin.GooglePlayServices.Ads.

    Here is a workaroud to fix this issue in the end of the page.

    Step 1. we need to create a new InterstitialCallback, If we use official InterstitialAdLoadCallback, we cannot override the OnAdLoaded method, because InterstitialAdLoadCallback forget to achieve the OnAdLoaded method from the AdLoadCallback, you can see the following screenshot. InterstitialAdLoadCallback do not have the OnAdLoaded method, it is unbelievable.

    129535-image.png
    129497-image.png

    Let's start our code to fix it. Create a new InterstitialCallback.cs.

    public abstract class InterstitialCallback : Android.Gms.Ads.Interstitial.InterstitialAdLoadCallback  
        {  
            [Register("onAdLoaded", "(Lcom/google/android/gms/ads/interstitial/InterstitialAd;)V", "GetOnAdLoadedHandler")]  
            public virtual void OnAdLoaded(Android.Gms.Ads.Interstitial.InterstitialAd interstitialAd)  
            {  
            }  
            private static Delegate cb_onAdLoaded;  
            private static Delegate GetOnAdLoadedHandler()  
            {  
                if (cb_onAdLoaded is null)  
                    cb_onAdLoaded = JNINativeWrapper.CreateDelegate((Action<IntPtr, IntPtr, IntPtr>)n_onAdLoaded);  
                return cb_onAdLoaded;  
            }  
            private static void n_onAdLoaded(IntPtr jnienv, IntPtr native__this, IntPtr native_p0)  
            {  
                InterstitialCallback thisobject = GetObject<InterstitialCallback>(jnienv, native__this, JniHandleOwnership.DoNotTransfer);  
                Android.Gms.Ads.Interstitial.InterstitialAd resultobject = GetObject<Android.Gms.Ads.Interstitial.InterstitialAd>(native_p0, JniHandleOwnership.DoNotTransfer);  
                thisobject.OnAdLoaded(resultobject);  
            }  
        }  
    

    Then back to the OnCreate method of MainActivity.cs. Using following code to load the InterstitialAd

    Android.Gms.Ads.Interstitial.InterstitialAd.Load(this, "ca-app-pub-1215741977455851/6629466575", new AdRequest.Builder().Build(), new InterstitialCallbackinherit());  
    

    In the end, please create a InterstitialCallbackinherit.cs to extend the InterstitialCallback,

    public class InterstitialCallbackinherit : InterstitialCallback  
        {  
              
            public override void OnAdLoaded(Android.Gms.Ads.Interstitial.InterstitialAd interstitialAd)  
            {  
                MainActivity.interstitialAd = interstitialAd;  
                interstitialAd.Show(MainActivity.instance);  
                base.OnAdLoaded(interstitialAd);  
            }  
            public override void OnAdFailedToLoad(LoadAdError p0)  
            {  
                base.OnAdFailedToLoad(p0);  
            }  
        }  
    

    MainActivity.instance is a public static MainActivity instance in the MainActivity.cs

    public class MainActivity : AppCompatActivity  
        {  
              
            public static MainActivity instance;  
            protected override void OnCreate(Bundle savedInstanceState)  
            {  
                base.OnCreate(savedInstanceState);  
                Xamarin.Essentials.Platform.Init(this, savedInstanceState);  
                
                SetContentView(Resource.Layout.activity_main);  
                instance = this;  
        }  
    

    Best Regards,
    Wenyan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Abdellah Amri 1 Reputation point
    2021-10-20T11:11:10.547+00:00

    Thank you for the answer but still the interstitiel ads does not show

    0 comments No comments