我正在使用xamarin创建一个android应用程序,并希望使用admob添加广告,我已经注册并创建了横幅单元,所以我有一个应用程序ID和一个广告ID。我已经按照一些指南实施了所有操作,但是在我要在设备上运行它之后,它将引发以下异常:
Java.Lang.RuntimeException
Message=Unable to get provider com.google.android.gms.ads.MobileAdsInitProvider: java.lang.IllegalStateException:
******************************************************************************
* The Google Mobile Ads SDK was initialized incorrectly. AdMob publishers *
* should follow the instructions here: googl link to add a valid *
* App ID inside the AndroidManifest. Google Ad Manager publishers should *
* follow instructions here: googl link. *
******************************************************************************
这里是我的AndroidManifest.xml的相关部分:
<meta-data android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-myappid"/> <!-- I have my actual app id here -->
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:exported="false"
android:theme="@android:style/Theme.Translucent" />
<provider android:name="com.google.android.gms.ads.MobileAdsInitProvider"
android:authorities="com.companyname.tapitmate.mobileadsinitprovider"
android:exported="false"
android:initOrder="100" />
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
这是我的adview类别:
public class AdControlView : View
{
}
这是我的视图渲染器:
public class AdViewRenderer : ViewRenderer<AdControlView, AdView>
{
//This is the test id according to the documentation
private readonly string _adUnitId = "ca-app-pub-3940256099942544/6300978111";
private readonly AdSize _adSize = AdSize.SmartBanner;
private AdView _adView;
public AdViewRenderer(Context context) : base(context)
{
}
AdView CreateAdView()
{
if (_adView != null)
return _adView;
_adView = new AdView(Context);
_adView.AdSize = _adSize;
_adView.AdUnitId = _adUnitId;
var adParams = new LinearLayout.LayoutParams(LayoutParams.WrapContent, LayoutParams.WrapContent);
_adView.LayoutParameters = adParams;
_adView.LoadAd(new AdRequest.Builder().Build());
return _adView;
}
protected override void OnElementChanged(ElementChangedEventArgs<AdControlView> e)
{
base.OnElementChanged(e);
if (Control is null)
{
CreateAdView();
SetNativeControl(_adView);
}
}
}
这是我的主要活动:
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
RequestedOrientation = ScreenOrientation.Portrait;
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
MobileAds.Initialize(ApplicationContext);// I have tried both ways, same exception.
//MobileAds.Initialize(ApplicationContext, "myactualappid");
LoadApplication(new App());
我正在使用:
我做错了什么?请帮忙。}
[很可能您只需要删除该MobileAdsInitProvider
:
<provider
android:name="com.google.android.gms.ads.MobileAdsInitProvider"
android:authorities="com.companyname.tapitmate.mobileadsinitprovider"
android:exported="false"
android:initOrder="100" />
documentation甚至没有它;因此,我假设您引用的是一个过时的示例。