简体   繁体   English

如何添加AdMob广告?

[英]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. 我一直在遵循本教程( http://www.kilobolt.com/day-7-creating-an-android-game-from-start-to-finish.html )来创建Android游戏。 Now I would like to add AdMob ads to the game in GameScreen Class inside private void drawGameOverUI() {...} . 现在,我想将AdMob广告添加到GameScreen类中的private void drawGameOverUI() {...}的游戏中。

I got the context from SampleGame Class using 我从SampleGame类获得上下文使用

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 private void drawGameOverUI() GameScreen类中,我有这个

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, "..."); 但是我遇到了以下错误: "Cannot resolve constructor 'AdView(android.content.Context, com.google.ads.AdSize, java.lang.String)'"(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, "..."); 在Google Developers( https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals#android )上,他们使用'this',但是当我使用(this, AdSize.BANNER, "..."); I got the error "Cannot resolve constructor 'AdView(com.name.GameScreen, com.google.ads.AdSize, java.lang.String)'" . 我收到错误"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 您的问题是contextGameScreen在

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

is not an instance of android.content.Context. 不是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. 仅提供断开连接的代码片段很难准确地确定正在执行的操作,但是您需要做的是为AdView构造函数提供将要嵌入其中的Activity。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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