简体   繁体   中英

How to add AdMob ads?

I've been following this tutorial ( http://www.kilobolt.com/day-7-creating-an-android-game-from-start-to-finish.html ) for creating an Android game. Now I would like to add AdMob ads to the game in GameScreen Class inside private void drawGameOverUI() {...} .

I got the context from SampleGame Class using

private static Context context;
public Screen getInitScreen() {
        SampleGame.context = getApplicationContext();
...
}
public static Context getAppContext() {
        return SampleGame.context;
}

In GameScreen Class inside private void drawGameOverUI() I have this

contextGameScreen = SampleGame.getAppContext();
LinearLayout layout = new LinearLayout(contextGameScreen);    
adView = new AdView(contextGameScreen, AdSize.BANNER, "...");
layout.addView(adView);
adView.loadAd(new AdRequest());

but I got this error "Cannot resolve constructor 'AdView(android.content.Context, com.google.ads.AdSize, java.lang.String)'" for (contextGameScreen, AdSize.BANNER, "..."); .

On Google Developers ( https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals#android ) they use 'this', but when I use (this, AdSize.BANNER, "..."); I got the error "Cannot resolve constructor 'AdView(com.name.GameScreen, com.google.ads.AdSize, java.lang.String)'" .

Can you please help me with this, how to solve this error, and to get this to work? This means alot to me. And also what exactly is 'this'?

Your problem is that contextGameScreen in

new AdView(contextGameScreen, AdSize.BANNER, "...")

is not an instance of android.content.Context. Eg an Activity or an Application.

It is very hard to work out exactly what you are doing as you have only provided disconnected scraps of code, but what you need to do is to provide the AdView constructor the Activity in which in will be embedded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM