Thank you for the answer but still the interstitiel ads does not show
Xamarin.Android : Admob v20 IntertitialAds problem
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
2 answers
Sort by: Newest
-
-
Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 26,336 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 officialInterstitialAdLoadCallback
, we cannot override theOnAdLoaded
method, becauseInterstitialAdLoadCallback
forget to achieve theOnAdLoaded
method from theAdLoadCallback
, you can see the following screenshot.InterstitialAdLoadCallback
do not have theOnAdLoaded
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); thisobject.OnAdLoaded(resultobject); } }
Then back to the
OnCreate
method ofMainActivity.cs
. Using following code to load the InterstitialAdAndroid.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 theInterstitialCallback
,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 publicstatic MainActivity instance
in theMainActivity.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.