• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

InterstitialAd's broken Android Studio 4.1.1

Kaplah

Lurker
Apr 16, 2021
1
0
New-ish android dev, 15 year experience C, C++, Java, script language dev.

Working with Android Studio 4.1.1 and trying to work out a basic scenario. I've built my main activity with just a button that feeds to a second activity that's a google admob interstitial ad.

The admob activity is just out of the box. I've not modified it from the template other than to give it a name.

Out the door it's using what I understand to be the old library:

import com.google.android.gms.ads.InterstitialAd

Which no longer exists. It should be...

import com.google.android.gms.ads.interstitial.InterstitialAd

Once I compile I get 8 errors. I'm assuming updates to the admob framework make this old template obsolete. I'm not sure what needs to be done to correct this. Is there an updated template I can snag, something that's communal knowledge, or does someone know what's missing from the outdated OOTB template that will get me working?

Here's the code, but I shouldn't need it as we're talking OOTB stuff.

Code:
package com.example.testapp

import com.google.android.gms.ads.AdListener
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.interstitial.InterstitialAd
import android.widget.Button
import android.widget.TextView

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast

// Remove the line below after defining your own ad unit ID.
private const val TOAST_TEXT = "Test ads are being shown. " +
        "To show live ads, replace the ad unit ID in res/values/strings.xml " +
        "with your own ad unit ID."
private const val START_LEVEL = 1

class ad1PostButtonPush : AppCompatActivity() {

    private var currentLevel: Int = 0
    private var interstitialAd: InterstitialAd? = null
    private lateinit var nextLevelButton: Button
    private lateinit var levelTextView: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_ad1_post_button_push)

        // Create the next level button, which tries to show an interstitial when clicked.
        nextLevelButton = findViewById(R.id.next_level_button)
        nextLevelButton.isEnabled = false
        nextLevelButton.setOnClickListener { showInterstitial() }

        levelTextView = findViewById(R.id.level)
        // Create the text view to show the level number.
        currentLevel = START_LEVEL

        // Create the InterstitialAd and set the adUnitId (defined in values/strings.xml).
        interstitialAd = newInterstitialAd()
        loadInterstitial()

        // Toasts the test ad message on the screen. Remove this after defining your own ad unit ID.
        Toast.makeText(this, TOAST_TEXT, Toast.LENGTH_LONG).show()
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.menu_ad1_post_button_push, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem) =
            when (item.itemId) {
                R.id.action_settings -> true
                else -> super.onOptionsItemSelected(item)
            }

    private fun newInterstitialAd(): InterstitialAd {
        return InterstitialAd().apply {
            adUnitId = getString(R.string.interstitial_ad_unit_id)
            adListener = object : AdListener() {
                override fun onAdLoaded() {
                    nextLevelButton.isEnabled = true
                }

                override fun onAdFailedToLoad(errorCode: Int) {
                    nextLevelButton.isEnabled = true
                }

                override fun onAdClosed() {
                    // Proceed to the next level.
                    goToNextLevel()
                }
            }
        }
    }

    private fun showInterstitial() {
        // Show the ad if it"s ready. Otherwise toast and reload the ad.
        if (interstitialAd?.isLoaded == true) {
            interstitialAd?.show()
        } else {
            Toast.makeText(this, "Ad did not load", Toast.LENGTH_SHORT).show()
            goToNextLevel()
        }
    }

    private fun loadInterstitial() {
        // Disable the next level button and load the ad.
        nextLevelButton.isEnabled = false
        val adRequest = AdRequest.Builder()
                .setRequestAgent("android_studio:ad_template")
                .build()
        interstitialAd?.loadAd(adRequest)
    }

    private fun goToNextLevel() {
        // Show the next level and reload the ad to prepare for the level after.
        levelTextView.text = "Level " + (++currentLevel)
        interstitialAd = newInterstitialAd()
        loadInterstitial()
    }
}

Here are the errors

...testapp\ad1PostButtonPush.kt: (62, 16): Cannot create an instance of an abstract class
...testapp\ad1PostButtonPush.kt: (63, 13): Val cannot be reassigned
...testapp\ad1PostButtonPush.kt: (64, 13): Unresolved reference: adListener
...testapp\ad1PostButtonPush.kt: (69, 17): 'onAdFailedToLoad' overrides nothing
...testapp\ad1PostButtonPush.kt: (83, 29): Unresolved reference: isLoaded
...testapp\ad1PostButtonPush.kt: (84, 34): No value passed for parameter 'p0'
...testapp\ad1PostButtonPush.kt: (97, 25): Unresolved reference: loadAd
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones