Search code examples
xmlandroid-layoutxamarinxamarin.formsadmob

Xamarin throw error : No view found for id 0x1 (unknown) for fragment ShellItemRenderer{...} I am trying to add ads to my application using AdMob


I am trying to add advertisements to my application using admob, but I am getting this error after running the application: "Java.Lang.IllegalArgumentException: 'No view found for id 0x1 (unknown) for fragment ShellItemRenderer{...}...

OnCreate in MainActivity.cs :

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>

What's problem?

Can you help me with this issue?


Solution

  • From the code you posted,I found that you are trying to use the AdView in your xamarin android project, but your app is a xamarin form app. So, you should be confused with the method of use.

    For xamarin android

    For how to add adview in xamarin android, you can check the AdMobExample Sample here. This sample demonstrates how to display different types of ads using Google AdMob with Google Play Services.

    For xamarin form

    On xamarin form, the steps to add admob to app are as follows:

    • Adding required packages.

      For Android,Add Xamarin.GooglePlayServices.Ads package.

      For iOS,Add Xamarin.Firebase.iOS.AdMob Package

    • Changes to be done in Manifest.xml file(only for Android)

    • Creating Custom Renders to access Ads for Banner type Ads

    • Accessing Custom Renders in the code to display Banner type Ads.

    • Creating Dependency Services to access Ads for Interstitial type Ads.

    • Accessing Dependency Services in the code to display Interstitial type Ads.

    For more information,you can check article Google AdMob - Display Ads In Xamarin Forms

    and AdMob In Xamarin.Forms – Display Google Ads In Your Mobile App.

    You can get an example here Adview sample of xamarin forms.