![](/img/trans.png)
[英]Xamarin android System.NullReferenceException: Object reference not set to an instance of an object
[英]“System.NullReferenceException: Object reference not set to an instance of an object” when trying to insert ads using google AdMob Xamarin Android
我一直在使用Android的xamarin上使用Ads时遇到问题,希望有更多专业知识的人可以提供一些建议。 这是我使用或至少尝试过使用Xamarin Android构建的第一个应用程序。 在版面配置中,我留了一个广告空间。 我遵循了多个指南,包括;
如何在最新的MonoGame Android(XNA)中集成AdMob广告?
https://blog.tommyparnell.com/admob-with-xamarin-part-1-banner-ads/
我遵循了这两个指南,每次尝试运行应用程序时都遇到相同的错误
我主要活动中的语法如下:
AdRequest adRequest = new AdRequest.Builder().Build();
layout.LoadAd(adRequest);
包括此内容时,将出现以下运行时错误;
Caused by: md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable: System.NullReferenceException: Object reference not set to an instance of an object
06-05 09:39:21.062 E/AndroidRuntime( 7212): at MyApp.Android.MainActivity.OnCreate (Android.OS.Bundle) [0x00028] in c:\Users\User\Dropbox\Projects\MyApp_Xamarin\MyApp.Android\MainActivity.cs:44
我尝试删除并重新创建Google Play服务参考。 我确保清单中有相关的活动标签
<activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
我完全不知所措。 我收到的错误看起来像Java错误,尽管由于我在c#环境中,所以无法直接访问和调试我可以告诉的错误。 经过3天没有解决此问题,我转向StackOverflow,如果有人可以指出正确的方向,我将不胜感激!
我正在使用Visual Studio 2013
干杯。
我已将admob添加到Xamarin Forms应用程序中,但未将其添加到XNA游戏中。 这是我在github上的应用程序的链接
var adMobElement = Element as AdMobBuddyControl;
if ((adMobElement != null) && (e.OldElement == null))
{
AdView ad = new AdView(this.Context);
ad.AdSize = AdSize.Banner;
ad.AdUnitId = adMobElement.AdUnitId;
var requestbuilder = new AdRequest.Builder();
ad.LoadAd(requestbuilder.Build());
this.SetNativeControl(ad);
}
确保在清单的应用程序标签中包含以下内容:
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
并且此权限:
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
我有点摸索着通过它。 您可能不需要我提到的所有内容,但希望它可以为您提供一个起点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.