Problems loading AdMob ads in app

I am trying to display a banner ad in my application and I seem to have a problem. Every time I enter the action, the application crashes and mentions something about it when I try to load an ad that it is an empty link. However, I am not sure why.

Here is the xml for the declaration:

<?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 declarations:

 //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 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 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 use the addTestDevice method, because I assume that this is what I need to use, since my application is not yet on the market. I have my admob account and I have the banner setup in this way.

Please let me know if you have any ideas. Many thanks.

+7
java android xml admob adview
source share
4 answers

R.id.bannerAd is not available in the content that you set for your activity / fragment. Before calling:

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

you should have called

 setContentView(R.layout.MyLayoutContainingBannerAd); 
+12
source share

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

+1
source share

I ran into a similar problem. When I carefully looked in the resource file. For my activity there were two resource files:

\ Res \ layout \ activity_main_lauch.xml

\ res \ layout v21 \ activity_main_lauch.xml

I modified one file, so it threw an error. when I apply changes to both files that it started working.

+1
source share

The decision I made did not solve this for me, since my setContentView() activity is actually in front of the ad code.

However, the specified ad was in the layout of the fragment that was inserted into the layout of the action. It seems that some kind of racing condition has occurred, because in the later code I posted the ad code, the more chances I had for success, although the code had little to do with the display.

I decided that the most appropriate place to put the code was in the onViewCreated() , where it had not let me down yet.

0
source share

All Articles