Xamarin.Android : Admob v20 IntertitialAds problem

Abdellah Amri 1 Reputation point

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


and some examples from this link but with no vain


Thank you

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


    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.


    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);  

    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;  
            public override void OnAdFailedToLoad(LoadAdError 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)  
                Xamarin.Essentials.Platform.Init(this, savedInstanceState);  
                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

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

    0 comments No comments