3

This is the onCreate(); method of Game.java activity of my 2d endless running game. I am New to java and android. I completed the game and is ready to publish to playstore. But I am unable to add admob banner to my game. admob tutorials i searched all ended up for .xml layout but my setContentView(); executes the java class. Please help me solve the problem.

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

}
theNirdosh
  • 63
  • 1
  • 5
  • possible duplicate of [How to create an admob banner programatically?](http://stackoverflow.com/questions/15953075/how-to-create-an-admob-banner-programatically) – hata Jul 25 '15 at 16:40
  • @hata The solution to that question is incorrect. `AdView adView = new AdView(activity, com.google.ads.AdSize.BANNER, );` cannot be applied to the constuctor `AdView(Context context, AttributeSet attrs, int defStyle)`. I see many solutions use that method, however. I've tried it myself before I learned. The API has changed. – pez Jul 25 '15 at 19:53

1 Answers1

5

Create the layout container programatically:

@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);
}

If you're seeing this error code: W/Ads﹕ Failed to load ad: 0:

Check that you are running the newest version of Google Play Services in build.gradle:

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

Check that you have the correct permissions in 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"/>

Check that you have the correct <meta-data> tag in Manifest.xml:

<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...

Check that you have the correct settings for your 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" />

Check that your ad unit id is correct. For testing purposes, you may use this unit id in res/strings.xml or elsewhere:

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

Full guidelines here.

pez
  • 3,859
  • 12
  • 40
  • 72
  • thank you for your reply but the code gets error. Error:(36, 25) error: no suitable constructor found for AdView(Game,AdSize,String) constructor AdView.AdView(Context,AttributeSet,int) is not applicable (actual argument AdSize cannot be converted to AttributeSet by method invocation conversion) constructor AdView.AdView(Context,AttributeSet) is not applicable (actual and formal argument lists differ in length) constructor AdView.AdView(Context) is not applicable (actual and formal argument lists differ in length) – theNirdosh Jul 25 '15 at 17:26
  • @theNirdosh Try it now – pez Jul 25 '15 at 18:03
  • @theNirdosh See my edit. Maybe you've forgotten one detail. – pez Jul 26 '15 at 18:27
  • Everything is in the place. My blue colored logs are: W/Ads﹕ There was a problem getting an ad response. ErrorCode: 0 W/dalvikvm﹕ VFY: unable to resolve virtual method 2940: Landroid/webkit/WebSettings;.setMixedContentMode (I)V W/dalvikvm﹕ VFY: unable to resolve virtual method 2951: Landroid/webkit/WebView;.evaluateJavascript (Ljava/lang/String;Landroid/webkit/ValueCallback;)V W/dalvikvm﹕ VFY: unable to resolve virtual method 6452: Lcom/google/android/gms/ads/internal/t/h;.isAttachedToWindow ()Z W/Ads﹕ Failed to load ad: 0 – theNirdosh Jul 26 '15 at 19:04
  • running same code to genymotion emulator instead of my phone give error: Failed to load ad: 1 – theNirdosh Jul 26 '15 at 19:53