marcojak / MTAdmob

Admob plugin for Xamarin Android and iOS
https://www.xamarinexpert.it/admob-made-easy/
160 stars 26 forks source link

App crashes on AppDelegate.LoadApplication() #121

Open KrizCologne opened 1 year ago

KrizCologne commented 1 year ago

After update to the latest version the iOS app crashes when calling LoadApplication in FinishedLaunching

Error:

[0:] An error occurred: 'Object reference not set to an instance of an object.'. Callstack: '  at MarcTron.Plugin.MTAdmobImplementation.GetRequest () <0x12e90c148 + 0x00024> in <518e4ffda48f4eb1896acaa8d2c1eb18>:0 
09:56:29:631      at MarcTron.Plugin.Services.InterstitialService.CreateInterstitialAd (System.String adUnit) <0x12e906f08 + 0x00002> in <518e4ffda48f4eb1896acaa8d2c1eb18>:0 
09:56:29:631      at MarcTron.Plugin.Services.InterstitialService.LoadInterstitial (System.String adUnit) <0x12e906840 + 0x0001c> in <518e4ffda48f4eb1896acaa8d2c1eb18>:0 
09:56:29:631      at MarcTron.Plugin.MTAdmobImplementation.LoadInterstitial (System.String adUnit) <0x12e906660 + 0x0000e> in <518e4ffda48f4eb1896acaa8d2c1eb18>:0 
09:56:29:631      at PLUTester.App..ctor () <0x12cc8ff60 + 0x0008c> in <9e25dfa95f7244e491e3b07e5f07f26f>:0 
09:56:29:631      at Blank.AppDelegate.FinishedLaunching (UIKit.UIApplication application, Foundation.NSDictionary launchOptions) [0x0000d] in C:\Users\CRoeder\source\repos\Privat\PLUTester\PLUTester\PLUTester.iOS\AppDelegate.cs:28 
09:56:29:631      at (wrapper managed-to-native) UIKit.UIApplication.xamarin_UIApplicationMain(int,string[],intptr,intptr,intptr&)
09:56:29:631      at UIKit.UIApplication.UIApplicationMain (System.Int32 argc, System.String[] argv, System.IntPtr principalClassName, System.IntPtr delegateClassName) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/16.0.0.92/src/Xamarin.iOS/UIKit/UIApplication.cs:57 
09:56:29:631      at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00013] in /Library/Frameworks/Xamarin.iOS.framework/Versions/16.0.0.92/src/Xamarin.iOS/UIKit/UIApplication.cs:82 
09:56:29:631      at PLUTester.iOS.Application.Main (System.String[] args) [0x00001] in C:\Users\CRoeder\source\repos\Privat\PLUTester\PLUTester\PLUTester.iOS\Main.cs:12 
09:56:29:631      at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&)
09:56:29:631      at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0006a] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/corlib/System.Reflection/RuntimeMethodInfo.cs:395 '

FinishedLaunching: public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions) { global::Xamarin.Forms.Forms.Init(); Rg.Plugins.Popup.Popup.Init(); LoadApplication(new App()); MobileAds.SharedInstance.Start(CompletionHandler); Firebase.Core.App.Configure(); return base.FinishedLaunching(application, launchOptions); }

Info.plist entrys:

    <key>GADApplicationIdentifier</key>
    <string>_secret_</string>
    <key>GADIsAdManagerApp</key>
    <true/>
marcojak commented 1 year ago

Probably something is not initialized yet while you try to load ad interstitial.

I would move MobileAds.SharedInstance.Start(CompletionHandler); before LoadApplication as I think you are loading an interstitial directly when you load your app. This might solve the problem.

KrizCologne commented 1 year ago

@marcojak When I do it that way, I got the following crash when calling MobileAds.SharedInstance.Start(CompletionHandler):

13:09:50:416    [0:] An error occurred: 'Object reference not set to an instance of an object.'. Callstack: '  at Blank.AppDelegate.FinishedLaunching (UIKit.UIApplication application, Foundation.NSDictionary launchOptions) [0x0000d] in C:\Users\CRoeder\source\repos\Privat\PLUTester\PLUTester\PLUTester.iOS\AppDelegate.cs:26 
13:09:50:416      at (wrapper managed-to-native) UIKit.UIApplication.xamarin_UIApplicationMain(int,string[],intptr,intptr,intptr&)
13:09:50:416      at UIKit.UIApplication.UIApplicationMain (System.Int32 argc, System.String[] argv, System.IntPtr principalClassName, System.IntPtr delegateClassName) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/16.0.0.92/src/Xamarin.iOS/UIKit/UIApplication.cs:57 
13:09:50:416      at UIKit.UIApplication.Main (System.String[] args, System.Type principalClass, System.Type delegateClass) [0x0003b] in /Library/Frameworks/Xamarin.iOS.framework/Versions/16.0.0.92/src/Xamarin.iOS/UIKit/UIApplication.cs:92 
13:09:50:416      at PLUTester.iOS.Application.Main (System.String[] args) [0x00001] in C:\Users\CRoeder\source\repos\Privat\PLUTester\PLUTester\PLUTester.iOS\Main.cs:12 
13:09:50:416      at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&)
13:09:50:416      at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0006a] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/corlib/System.Reflection/RuntimeMethodInfo.cs:395 '
Rhyno950 commented 1 year ago

Have you found a solution to this issue? I'm having the same problem, MobileAds.SharedInstance is null. I tried calling MobileAds.SharedInstance.Start(CompletionHandler); from a dependency service at a later point in time but before a view with an ad is opened as a work around and MobileAds.SharedInstance is still null. Using the test app ID in info.p.

Rhyno950 commented 1 year ago

So it turns out SharedInstance is null when building from Visual Studios on windows. Not ideal but works fine when building from a mac