8

I am trying to have an ad banner display on my app and I seem to be having an issue. Anytime I go into the activity, the app crashes and mentions something about that when I am trying to load the ad, that it is a null reference. I am not positive why though.

Here is the xml for the adview:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/gameLayout">

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

</FrameLayout>

Here is the java code for using the ads:

//load ads
AdView adView = (AdView)findViewById(R.id.bannerAd);
AdRequest.Builder request = new AdRequest.Builder();
request.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
adView.loadAd(request.build());

Here is the logcat:

09-21 21:55:39.300: E/AndroidRuntime(1168): FATAL EXCEPTION: main
09-21 21:55:39.300: E/AndroidRuntime(1168): Process: com.project.llb, PID: 1168
09-21 21:55:39.300: E/AndroidRuntime(1168): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.twentytwentythree.sab/com.twentytwentythree.sab.runGraphics}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.ads.AdView.loadAd(com.google.android.gms.ads.AdRequest)' on a null object reference
09-21 21:55:39.300: E/AndroidRuntime(1168):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
09-21 21:55:39.300: E/AndroidRuntime(1168):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)
09-21 21:55:39.300: E/AndroidRuntime(1168):     at android.app.ActivityThread.access$800(ActivityThread.java:156)
09-21 21:55:39.300: E/AndroidRuntime(1168):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
09-21 21:55:39.300: E/AndroidRuntime(1168):     at android.os.Handler.dispatchMessage(Handler.java:102)
09-21 21:55:39.300: E/AndroidRuntime(1168):     at android.os.Looper.loop(Looper.java:157)
09-21 21:55:39.300: E/AndroidRuntime(1168):     at android.app.ActivityThread.main(ActivityThread.java:5872)
09-21 21:55:39.300: E/AndroidRuntime(1168):     at java.lang.reflect.Method.invoke(Native Method)
09-21 21:55:39.300: E/AndroidRuntime(1168):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:852)
09-21 21:55:39.300: E/AndroidRuntime(1168):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:668)
09-21 21:55:39.300: E/AndroidRuntime(1168): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.ads.AdView.loadAd(com.google.android.gms.ads.AdRequest)' on a null object reference
09-21 21:55:39.300: E/AndroidRuntime(1168):     at com.twentytwentythree.sab.runGraphics.onCreate(runGraphics.java:94)
09-21 21:55:39.300: E/AndroidRuntime(1168):     at android.app.Activity.performCreate(Activity.java:5312)
09-21 21:55:39.300: E/AndroidRuntime(1168):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
09-21 21:55:39.300: E/AndroidRuntime(1168):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2552)
09-21 21:55:39.300: E/AndroidRuntime(1168):     ... 9 more

Here is the code from the androidmanifest:

<meta-data android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />
    <activity
        android:name="com.google.android.gms.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation" >
    </activity>

Now I am using the addTestDevice method because I assume that is what I need to use since my app is not on the market place yet. I do have my admob account setup and I have the banner setup that way.

Please let me know if you have any ideas. Thanks a lot.

sboehnke
  • 287
  • 1
  • 3
  • 13

4 Answers4

14

R.id.bannerAd is not available in the content you have set for your Activity/Fragment. Prior to calling:

AdView adView = (AdView)findViewById(R.id.bannerAd);

you must have called

setContentView(R.layout.MyLayoutContainingBannerAd);
William
  • 19,567
  • 8
  • 45
  • 88
  • After doing that now I have a box that comes up saying "Missing AdActivity with android.configChanges in AndroidManifest.xml" In my manifest i have the meta-data tag set with com.google.android.gms.version and the activity has a name com.google.android.gms.ads.AdActivity and configChanges to keyboard|keyboardHidden|orientation. No idea what the problem is. – sboehnke Sep 22 '14 at 23:55
  • You need to post your AndroidManifest, but it sounds like you have not specified all of the config changes that are required for the AdActivity. – William Sep 23 '14 at 00:29
  • Added keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize to my configChanges and it works now. – sboehnke Sep 23 '14 at 01:07
  • That method should be setContentView() not setContent() right? – wasimsandhu Apr 25 '15 at 21:35
1

I faced Similar issue. When i checked closely in resourse file There were two resourse file for my activity:

\res\layout\activity_main_lauch.xml

\res\layout-v21\activity_main_lauch.xml

I was modifing single file, hence it was throwing error. when i apply the change in both files it started working.

0

Please check you have included - compile 'com.google.android.gms:play-services-ads:8.4.0' in the gradle.

Preetham Hegde
  • 759
  • 1
  • 12
  • 22
0

The accepted solution did not solve this for me as my Activity's setContentView() is indeed before the ad code.

The ad in question, however, was in a fragment layout, which was being inserted into the activity's layout. It seems that some sort of race condition was happening, in that the later in the code I placed the ad code the more chance I had for success, even though the code had little to do with the display.

I decided the most satisfactory place to put the code was in the fragment's onViewCreated(), where it has not failed me yet.

Ger
  • 579
  • 1
  • 10
  • 20