【问题标题】:How to create admob banner programatically with setContentView(new Activity(this)); without using xml?如何使用 setContentView(new Activity(this)) 以编程方式创建 admob 横幅;不使用xml?
【发布时间】:2015-10-16 04:48:15
【问题描述】:

这是 onCreate();我的 2d 无尽运行游戏的 Game.java 活动方法。我是java和android的新手。我完成了游戏并准备发布到 Playstore。但我无法在我的游戏中添加 admob 横幅。我搜索的 admob 教程都以 .xml 布局结束,但我的 setContentView();执行java类。请帮我解决问题。

public class Game extends Activity {

MediaPlayer bgsound;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //turn title off
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    //set to full screen
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);


    setContentView(new GamePanel(this));



    //sound
    bgsound = MediaPlayer.create(Game.this, R.raw.bgsound);
    bgsound.setLooping(true);
    bgsound.start();

}

【问题讨论】:

  • @hata 该问题的解决方案不正确。 AdView adView = new AdView(activity, com.google.ads.AdSize.BANNER, <publisher_id>); 不能应用于构造函数 AdView(Context context, AttributeSet attrs, int defStyle)。但是,我看到许多解决方案都使用该方法。在我学习之前,我自己尝试过。 API 已更改。

标签: java android android-layout android-studio admob


【解决方案1】:

以编程方式创建布局容器:

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.VERTICAL);

    // Create a banner ad
    mAdView = new AdView(this);
    mAdView.setAdSize(AdSize.SMART_BANNER);
    mAdView.setAdUnitId("myAdUnitId");

    // Create an ad request.
    AdRequest.Builder adRequestBuilder = new AdRequest.Builder();

    // Optionally populate the ad request builder.
    adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);

    // Add the AdView to the view hierarchy.
    layout.addView(mAdView);

    // Start loading the ad.
    mAdView.loadAd(adRequestBuilder.build());

    setContentView(layout);
}

如果您看到此错误代码:W/Ads﹕ Failed to load ad: 0:

检查您是否在build.gradle 中运行最新版本的 Google Play 服务:

dependencies {
    compile 'com.google.android.gms:play-services-ads:7.5.0'
    }

检查您在Manifest.xml 中是否拥有正确的权限:

<!-- Include required permissions for Google Mobile Ads to run-->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

检查Manifest.xml 中的&lt;meta-data&gt; 标签是否正确:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <!--This meta-data tag is required to use Google Play Services.-->
    <meta-data android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />
    <activity...

检查您的AdActivity 的设置是否正确:

<!--Include the AdActivity configChanges and theme. -->
<activity android:name="com.google.android.gms.ads.AdActivity"
    android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
    android:theme="@android:style/Theme.Translucent" />

检查您的广告单元 ID 是否正确。出于测试目的,您可以在 res/strings.xml 或其他地方使用此单元 ID:

<string name="banner_ad_unit_id">ca-app-pub-3940256099942544/6300978111</string>

完整指南here

【讨论】:

  • 感谢您的回复,但代码出错。 Error:(36, 25) error: no applicable constructor found for AdView(Game,AdSize,String) 构造函数 AdView.AdView(Context,AttributeSet,int) 不适用(实参AdSize无法通过方法调用转换为AttributeSet)构造函数 AdView.AdView(Context,AttributeSet) 不适用(实际参数列表和形式参数列表长度不同) 构造函数 AdView.AdView(Context) 不适用(实际参数列表和形式参数列表长度不同)
  • @theNirdosh 立即尝试
  • @theNirdosh 查看我的编辑。也许你忘记了一个细节。
  • 一切都在原地。我的蓝色日志是: W/Ads:获得广告响应时出现问题。 ErrorCode: 0 W/dalvikvm: VFY: 无法解析虚拟方法 2940: Landroid/webkit/WebSettings;.setMixedContentMode (I)VW/dalvikvm: VFY: 无法解析虚拟方法 2951: Landroid/webkit/WebView;.evaluateJavascript (Ljava /lang/String;Landroid/webkit/ValueCallback;)VW/dalvikvm: VFY: 无法解析虚拟方法 6452: Lcom/google/android/gms/ads/internal/t/h;.isAttachedToWindow ()ZW/Ads: 失败加载广告:0
  • 在 genymotion 模拟器而不是我的手机上运行相同的代码给出错误:无法加载广告:1
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多