Question

Im trying to integrate admob ads into my app and I've been trying to follow the tutorials on Google's APIs and their code is causing my entire app to crash.

I want to think it's because I'm testing on my phone and I havent included adRequest.addTestDevice("TEST_DEVICE_ID"); because to obtain your device id you first need to request an ad, and I am failing exactly where I am requesting an ad.

This is what I have in my main.java taken line for line from androids api tutorials. Log cat says it's crashing at adView.loadAd(adRequest);

 // Look up the AdView as a resource and load a request.
    AdView adView = (AdView)this.findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);

Here's the XML

 <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="adUnitId"
        />

and finally here is what log cat has to say: http://imgur.com/UfB45YF

referenced api: https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals#play

Do you think it's crashing because I don't have my test device ID included? Or is there something else happening?

Was it helpful?

Solution

If it crashing on adView.loadAd with a NullPointerException then you have no View called R.id.adView in that Activity's layout.

Look at your Activity's onCreate and what layout you have specified in setContentView. and make sure that you aren't also using something like libGDX AndroidApplication which sets it own ContentView.

Finally do a clean build.

OTHER TIPS

If you are putting the code in MainActivity.java and you have multiple layouts associated ie:

activity_main.xml sw600dp\activity_main.xml sw720dp\activity_main.xml

then you need to put the com.google.android.gms.ads.AdView xml code in each layout regardless of what device you are testing for.

That was my problem. Hope this helps others. Also be sure the set your dependencies and change your manifest.

I had the same problem. When I was debugging I saw an error about the google play services version number being incorrect, and it showed the real value. I added this value to the strings.xml file:

<integer name="google_play_services_version">7095000</integer>

Once it was in, everything ran fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top