首页 > 解决方案 > 提取原生广告时出现“广告加载失败:3”的一些原因是什么?

问题描述

我正在尝试为 Google AdMob 原生广告使用原生模板,但我什至无法加载测试广告。我的主要活动如下所示:

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        MobileAds.initialize(this) {}
        var adLoader: AdLoader = AdLoader.Builder(this, ADMOB_AD_UNIT_ID)
            .forUnifiedNativeAd { ad: UnifiedNativeAd ->
                var styles: NativeTemplateStyle =
                    NativeTemplateStyle.Builder().build()
                var template: TemplateView = findViewById(R.id.my_template)
                template.setStyles(styles)
                template.setNativeAd(ad)
            }
            .withAdListener(object : AdListener() {
                override fun onAdFailedToLoad(adError: LoadAdError) {
                    print("Failed: " + adError.toString())
                    // Handle the failure by logging, altering the UI, and so on.
                }
            })
            .withNativeAdOptions(
                NativeAdOptions.Builder()
                    // Methods in the NativeAdOptions.Builder class can be
                    // used here to specify individual options settings.
                    .build()
            )
            .build()

        adLoader.loadAd(AdRequest.Builder().build());
    }
}

虽然我AndroidManifest.xml看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.admobtest">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-3940256099942544~3347511713"/>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

应用程序 ID 和单元 ID 均来自示例项目。没有编译错误;看起来 AdMob 记录“广告加载失败:3”。我已经读到这个问题随着时间的推移而得到解决,但这不是我的应用程序/单元 ID,所以它绝对不应该发生。这些 id 适用于上述示例项目,所以我的代码肯定有问题,但我不知道它可能是什么。

标签: androidkotlinadmobnative-ads

解决方案


推荐阅读