idris
idris

Reputation: 63

incompatible types: String cannot be converted to OnInitializationCompleteListener

I am using android Studio 2021.1 and strangely after updating my libraries earlier I get this error when I compile my application incompatible types: String cannot be converted to OnInitializationCompleteListener

MobileAds.initialize(this, getString(R.string.admob_banner_id));
mAdView = findViewById(R.id.adView);     //The line that returns the error to me
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);

Upvotes: 2

Views: 1593

Answers (3)

idris
idris

Reputation: 63

Apparently google has changed the way of integrating ads into apps. I went to the site and applied the new rules to my code and everything was back to normal.

For those who will encounter the same problem simply replace your code with this one

AdView adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(getString(R.string.admob_banner_id));

Upvotes: 1

Anatolii Chub
Anatolii Chub

Reputation: 1300

MobileAds.initialize() has a signature:

public static void initialize (Context context, OnInitializationCompleteListener listener)

It receiving an instance of OnInitializationCompleteListener as a second argument. In your code you try to send a string as a second parameter. You should change second parameter in this line

MobileAds.initialize(this, getString(R.string.admob_banner_id));

to instance of OnInitializationCompleteListener.

Upvotes: 1

Elango
Elango

Reputation: 422

Typecasting

mAdView = (AdView)findViewById(R.id.adView);

Upvotes: 1

Related Questions