【问题标题】:Implementing admob in xamarin android throws Java.Lang.RuntimeException在 xamarin android 中实现 admob 抛出 Java.Lang.RuntimeException
【发布时间】:2021-04-12 05:03:21
【问题描述】:

我正在使用 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" />

这是我的广告视图类:

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());

我正在使用:

  • Xamarin.Essentials 1.5.1
  • Xamarin.Forms 4.5.0.396
  • Xamarin.GooglePlayServices.Ads 71.1720.1

我做错了什么?请帮忙。 }

【问题讨论】:

标签: android xamarin xamarin.android admob


【解决方案1】:

您好像错过了 ExportRenderer

[assembly: ExportRenderer(typeof(AdControlView), typeof(AdViewRenderer))]

在 mainactivity 中,下面的行应该放在 Forms.Init 行之前,你在 Init 之后添加了

  MobileAds.Initialize(ApplicationContext, "ca-app-pub-3940256099942544/6300978111");

您的清单应该在应用程序元素的元数据元素中包含adUnitId。不确定,因为整个清单还没有在这里发布。 Like here

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.samplemtadmob">
    <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="28" />
    <application android:label="SampleMTAdmob.Android">
      <meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="ca­-app-pub-­3940256099942544~3347511713"/>
      <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>
      <activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:theme="@android:style/Theme.Translucent" />
    </application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>

还有

重要

为了在开发过程中测试横幅,google 使用了两个横幅 ID,一个用于 Android >,另一个用于 iOS。使用它们,然后记得用你自己的 ID 替换它们:

Android:ca-app-pub-3940256099942544/6300978111 iOS:ca-app-pub-3940256099942544/2934735716

试试这个 nuget 而不是实现渲染器marcojak/MTAdmob

【讨论】:

  • 我按照你的回答做了所有事情,它不再抛出异常,但它仍然无法正常工作。它什么也不做,甚至没有点击 OnCreate 方法,只是显示一个白色背景并在几分钟后自动停止调试,没有任何错误。我也用给定的 nuget 进行了尝试,结果相同。
  • 没关系,它是这样工作的,这只是我的视觉工作室的东西,谢谢你的帮助。
【解决方案2】:

很可能你只需要删除那个MobileAdsInitProvider

<provider
    android:name="com.google.android.gms.ads.MobileAdsInitProvider" 
    android:authorities="com.companyname.tapitmate.mobileadsinitprovider" 
    android:exported="false" 
    android:initOrder="100" />

documentation 甚至没有它;因此我假设你指的是一个过时的例子。

【讨论】:

  • “它不工作”是什么意思?错误消息至少应该是另一个;它必须通过代码初始化:MobileAds.Initialize(ApplicationContext, "ca-app-pub-...");.
  • 它甚至没有达到那条线......它只是在加载应用程序时抛出错误
【解决方案3】:

当我将广告更新到版本 71.1720.1 时遇到了这个问题,回滚到版本 60.1142.1 解决了这个问题,因为我必须对 Xamarin.maps 做同样的事情

【讨论】:

    猜你喜欢
    • 2017-05-24
    • 2022-01-02
    • 2013-04-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多