1

I'm trying to use the Native Templates for Google AdMob native ads, but I can't even get test ads to load. My main activity looks like this:

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

while my AndroidManifest.xml looks like this:

<?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>

The application id and the unit id are both from this sample project. There's no compilation error; it looks like AdMob logs "Ad failed to load : 3". I've read that this issue is resolved with time, but this isn't my app/unit id, so it should definitely not be happening. These id's work for the aforementioned sample project, so there must be a problem with my code, but I have no clue what it could be.

user3210986
  • 211
  • 2
  • 9

0 Answers0