繁体   English   中英

如何添加AdMob广告?

[英]How to add AdMob ads?

我一直在遵循本教程( http://www.kilobolt.com/day-7-creating-an-android-game-from-start-to-finish.html )来创建Android游戏。 现在,我想将AdMob广告添加到GameScreen类中的private void drawGameOverUI() {...}的游戏中。

我从SampleGame类获得上下文使用

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

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

但是我遇到了以下错误: "Cannot resolve constructor 'AdView(android.content.Context, com.google.ads.AdSize, java.lang.String)'"(contextGameScreen, AdSize.BANNER, "...");

在Google Developers( https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals#android )上,他们使用'this',但是当我使用(this, AdSize.BANNER, "..."); 我收到错误"Cannot resolve constructor 'AdView(com.name.GameScreen, com.google.ads.AdSize, java.lang.String)'"

您能帮我解决这个错误,如何使它正常工作吗? 这对我来说意味着很多。 而且“这”到底是什么?

您的问题是contextGameScreen在

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

不是android.content.Context的实例。 例如活动或应用程序。

仅提供断开连接的代码片段很难准确地确定正在执行的操作,但是您需要做的是为AdView构造函数提供将要嵌入其中的Activity。

暂无
暂无

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

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