我尝试使用admob向我的应用程序添加广告,但运行应用程序后出现此错误:“Java.Lang.IllegalArgumentException:'未找到片段ShellItemRenderer{...}的ID 0x 1(未知)的视图...
MainActivity.cs中的OnCreate:
protected override void OnCreate(Bundle savedInstanceState)
{
FFImageLoading.Forms.Platform.CachedImageRenderer.Init(true);
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
MobileAds.Initialize(ApplicationContext);
string adUnitId = Resources.GetString(Resource.String.banner_ad_unit_id);
SetContentView(Resource.Layout.activity_main);
AdView adView = FindViewById<AdView>(Resource.Id.adView);
var adRequest = new AdRequest.Builder().Build();
adView.LoadAd(adRequest);
LoadApplication(new App());
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-***" />
</RelativeLayout>
有什么问题?
你能帮我解决这个问题吗?
1条答案
按热度按时间f1tvaqid1#
从你发布的代码中,我发现你试图在你的xamarin android项目中使用
AdView
,但你的应用程序是一个xamarin表单应用程序。所以,你应该对使用方法感到困惑。对于xamarin android
关于如何在xamarin android中添加广告视图,可以查看这里的AdMobExample Sample。本示例演示如何使用
Google AdMob
和Google Play Services
显示不同类型的广告。对于xamarin表单
在xamarin表单上,将admob添加到app的步骤如下:
对于Android,添加
Xamarin.GooglePlayServices.Ads
包。对于iOS,添加
Xamarin.Firebase.iOS.AdMob
包Custom Renders
以访问横幅广告类型的广告Custom Renders
以显示Banner类型的广告。Dependency Services
以访问插播式广告的广告。Dependency Services
以显示间隙类型广告。有关详细信息,可以查看文章Google AdMob - Display Ads In Xamarin Forms
AdMob In Xamarin.Forms – Display Google Ads In Your Mobile App。
你可以在这里得到一个例子Adview sample of xamarin forms。