Xamarin抛出错误:未找到片段ShellItemRenderer{...}的id 0x 1(未知)的视图我正在尝试使用AdMob将广告添加到我的应用程序

piv4azn7  于 2023-04-18  发布在  Shell
关注(0)|答案(1)|浏览(89)

我尝试使用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>

有什么问题?
你能帮我解决这个问题吗?

f1tvaqid

f1tvaqid1#

从你发布的代码中,我发现你试图在你的xamarin android项目中使用AdView,但你的应用程序是一个xamarin表单应用程序。所以,你应该对使用方法感到困惑。

对于xamarin android

关于如何在xamarin android中添加广告视图,可以查看这里的AdMobExample Sample。本示例演示如何使用Google AdMobGoogle Play Services显示不同类型的广告。

对于xamarin表单

在xamarin表单上,将admob添加到app的步骤如下:

  • 添加所需的包。

对于Android,添加Xamarin.GooglePlayServices.Ads包。
对于iOS,添加Xamarin.Firebase.iOS.AdMob

  • 要在Manifest.xml文件中完成的更改(仅适用于Android)
  • 创建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

相关问题