Admob crash even when adUnitId and adSize is set before onload.

2,600 views
Skip to first unread message

subhash pandey

unread,
Nov 30, 2015, 8:12:40 AM11/30/15
to Google Mobile Ads SDK Developers
Followed below link to implement :

When adUnitId is set in XML it works fine, but when setting via code app crashes, (mAdView.getAdSize(); mAdView.getAdUnitID() are also in healthy state.


java code
mAdView = (AdView) findViewById(R.id.adView);
mAdView.setAdUnitId(SaharaConfiguration.getInstance().getAD_UNIT_ID());
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);

XML code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/color_base_layout">

<FrameLayout
android:id="@+id/fragment_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/adView"/>

<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</com.google.android.gms.ads.AdView>

</RelativeLayout>

gradle dependency 
compile 'com.google.android.gms:play-services-ads:8.3.0'
compile 'com.android.support:support-v4:21.1.0'
compile 'com.google.android.gms:play-services:8.3.0'



 FATAL EXCEPTION: main
                                                                                       Process: com.grahamdigital.reach.taxslayerbowl, PID: 32025
                                                                                       java.lang.RuntimeException: Unable to start activity ComponentInfo{packagename.HomeActivity}: java.lang.IllegalStateException: The ad size and ad unit ID must be set before loadAd is called.
                                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                                           at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                                           at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                           at android.os.Looper.loop(Looper.java:148)
                                                                                           at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                                        Caused by: java.lang.IllegalStateException: The ad size and ad unit ID must be set before loadAd is called.
                                                                                           at com.google.android.gms.ads.internal.client.zzz.zzda(Unknown Source)
                                                                                           at com.google.android.gms.ads.internal.client.zzz.zza(Unknown Source)
                                                                                           at com.google.android.gms.ads.BaseAdView.loadAd(Unknown Source)
                                                                                           at com.google.android.gms.ads.AdView.loadAd(Unknown Source)
                                                                                           at com.grahamdigital.sahara.activity.HomeActivity.onCreate(HomeActivity.java:91)
                                                                                           at android.app.Activity.performCreate(Activity.java:6237)
                                                                                           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                                           at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                                           at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                           at android.os.Looper.loop(Looper.java:148) 
                                                                                           at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                                           at java.lang.reflect.Method.invoke(Native Method) 
                                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

subhash pandey

unread,
Nov 30, 2015, 8:15:24 AM11/30/15
to Google Mobile Ads SDK Developers
also running on Nexus 5 OS 6.0

Veer Arjun Busani

unread,
Nov 30, 2015, 4:01:42 PM11/30/15
to Google Mobile Ads SDK Developers
Hey Subhash,

Have you tried replicating the issue using our sample app? I have been able to successfully use a Singleton to store and retrieve the Ad-Unit Id. For reference, please use the following code and let us know of any questions you may have for us. 

You may use your Singleton as follows:
public class AdUnit {
public String adUnitId = "ca-app-pub-3940256099942544/6300978111";
private Static AdUnit ourInstance = new AdUnit();
public static AdUnit getInstance() {
return ourInstance;
}
}
Thanks,
Veer Arjun Busani
Mobile Ads SDK Team

subhash pandey

unread,
Dec 1, 2015, 1:10:54 AM12/1/15
to google-adm...@googlegroups.com
Admob Sample dose't even run, gives below error, I tried different buildToolVersion to build it, no luck.


Information:Gradle tasks [clean, :app:generateDebugSources, :app:generateDebugAndroidTestSources, :app:compileDebugSources, :app:compileDebugAndroidTestSources]
:app:clean
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72301Library
:app:prepareComAndroidSupportSupportV42301Library
:app:prepareComGoogleAndroidGmsPlayServicesAds810Library
:app:prepareComGoogleAndroidGmsPlayServicesAppindexing810Library
:app:prepareComGoogleAndroidGmsPlayServicesBasement810Library
:app:prepareDebugDependencies
:app:compileDebugAidl
:app:compileDebugRenderscript
:app:generateDebugBuildConfig
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources
:app:mergeDebugResources
:app:processDebugManifest
:app:processDebugResources FAILED
Error:Execution failed for task ':app:processDebugResources'.
> at index 4
Information:BUILD FAILED
Information:Total time: 4.66 secs
Information:1 error
Information:0 warnings
Information:See complete output in console


--

---
You received this message because you are subscribed to a topic in the Google Groups "Google Mobile Ads SDK Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-admob-ads-sdk/dA34NCMVG_4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-admob-ads...@googlegroups.com.
To post to this group, send email to google-adm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Regards
Subhash Pandey
9654580179

subhash pandey

unread,
Dec 1, 2015, 1:40:56 AM12/1/15
to google-adm...@googlegroups.com
Build error is Android Studio 1.5 specific, on 1.4 it build and crash saying same earlier reported issue "java.lang.IllegalStateException: The ad size and ad unit ID must be set before loadAd is called" .
when trying to set adUnitId in java code.

Veer Arjun Busani

unread,
Dec 1, 2015, 10:15:37 AM12/1/15
to Google Mobile Ads SDK Developers
Hi Subhash,

I understand that you are trying to load the AdView from XML and then set adUnitId to request the banner. By adding into the activity's layout via XML, AdView requires both ad size and ad unit ID parameters to be set before you can load an instance of that view into your activity. If you want to set the ad unit ID via your class, you can do the following:

Let's say that you have a RelativeLayout in the XML for the activity. Name it as adRelativeLayout and in your class:

mAdRelativeLayout = (RelativeLayout) findViewById(R.id.adRelativeLayout);
AdView adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
adView.setLayoutParams(params);

mAdRelativeLayout.addView(adView);
adView.setAdUnitId(AdUnit.getInstance().adUnitId);

AdRequest adRequest = new AdRequest.Builder().build();

adView.loadAd(adRequest);    

Thanks,
Veer Arjun Busani
Mobile Ads SDK Team

To unsubscribe from this group and all its topics, send an email to google-admob-ads-sdk+unsub...@googlegroups.com.

To post to this group, send email to google-adm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Regards
Subhash Pandey
9654580179

subhash pandey

unread,
Dec 2, 2015, 12:03:13 AM12/2/15
to google-adm...@googlegroups.com
Thanks it worked, but its strange to crash when we try to set adUnitId on hard coded XML.

To unsubscribe from this group and all its topics, send an email to google-admob-ads...@googlegroups.com.
To post to this group, send email to google-adm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Regards
Subhash Pandey
9654580179



--
Regards
Subhash Pandey
9654580179

--

---
You received this message because you are subscribed to a topic in the Google Groups "Google Mobile Ads SDK Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-admob-ads-sdk/dA34NCMVG_4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-admob-ads...@googlegroups.com.

To post to this group, send email to google-adm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages