Xamarin集成AdMob报错:找不到ShellItemRenderer对应的视图ID 0x1
问题分析与解决方案
错误根源
- 调用顺序冲突:你在
OnCreate中先通过SetContentView(Resource.Layout.activity_main)加载了自定义原生布局,随后调用LoadApplication(new App())——而Xamarin.Forms的LoadApplication会自动替换当前Activity的ContentView为Forms专属的容器视图,这直接导致Shell渲染器找不到它依赖的容器(错误中的id 0x1是Forms默认容器的id),从而抛出异常。 - 集成方式不符合Xamarin.Forms架构:直接在原生Activity中操作布局添加AdView,破坏了跨平台UI的统一管理逻辑,不是推荐的做法。
解决方案
方案一:保留原生布局+Forms内容共存(适合原生层叠加广告场景)
- 修改
activity_main.xml,添加一个FrameLayout作为Xamarin.Forms内容的承载容器:
<?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"> <!-- 承载Xamarin.Forms内容的容器 --> <FrameLayout android:id="@+id/forms_content" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@id/adView"/> <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>
- 调整
MainActivity.cs的OnCreate方法,指定Forms加载到新增的FrameLayout中:
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); // 先设置自定义布局 SetContentView(Resource.Layout.activity_main); // 初始化并加载广告 AdView adView = FindViewById<AdView>(Resource.Id.adView); var adRequest = new AdRequest.Builder().Build(); adView.LoadAd(adRequest); // 指定Forms内容加载到指定的FrameLayout容器 LoadApplication(new App(), FindViewById<FrameLayout>(Resource.Id.forms_content)); }
方案二:使用Xamarin.Forms官方推荐的跨平台集成方式(更规范)
这种方式完全在Forms层管理广告,符合跨平台架构:
- 安装NuGet包:
Google.MobileAds(确保选择支持Xamarin.Forms的版本) - 调整
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); LoadApplication(new App()); }
- 在Xamarin.Forms的XAML页面中直接添加AdView控件:
<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:ads="clr-namespace:Google.MobileAds;assembly=Google.MobileAds" x:Class="YourApp.MainPage"> <RelativeLayout> <!-- 页面主体内容区域 --> <StackLayout RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width}" RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=1, Constant=-50}"> <!-- 这里放置你的页面控件 --> </StackLayout> <!-- AdMob横幅广告 --> <ads:AdView AdUnitId="ca-app-pub-***" AdSize="Banner" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width}" RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=1, Constant=-50}"/> </RelativeLayout> </ContentPage>
内容的提问来源于stack exchange,提问作者mozal
相关产品推荐
相关产品推荐

