Android - Google Play Service throw IllegalStateException complaining about size and ad unit id

578 views
Skip to first unread message

Adelino Lobão

unread,
May 17, 2014, 4:46:15 AM5/17/14
to google-adm...@googlegroups.com
Since I replaced the Admob library for the Google Play Service library, my Android app sometimes triggers the following exception

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.XXX.XXX/com.XXX.XXX.ui.activity.FeedActivity}: java.lang.IllegalStateException: The ad size and ad unit ID must be set before loadAd is called.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2328)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2386)
at android.app.ActivityThread.access$900(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5476)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: The ad size and ad unit ID must be set before loadAd is called.
at com.google.android.gms.internal.ak.an(Unknown Source)
at com.google.android.gms.internal.ak.a(Unknown Source)
at com.google.android.gms.ads.AdView.loadAd(Unknown Source)
at com.XXX.newslibrary.ui.activity.FeedActivity.initActivityLayout(FeedActivity.java:255)
at com.XXX.newslibrary.ui.activity.FeedActivity.onCreate(FeedActivity.java:72)
at android.app.Activity.performCreate(Activity.java:5451)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
... 11 more

Basically the exception is complaining that I'm not setting the ad size and unit id before loadAd. This is me code:

    mAdView = new AdView(this);
    mAdView.setAdSize(AdSize.BANNER);
    mAdView.setAdUnitId(SourceActivity.APPLICATION_ADMOB_BANNER_ID);

    LinearLayout layout = (LinearLayout) findViewById(R.id.activity_view);
    layout.addView(mAdView);

    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);
    mAdView.setVisibility(View.GONE);

    mAdView.setAdListener(new AdListener() {
      @Override
      public void onAdLoaded() {
          Log.d(TAG, "onAdLoaded");
          mAdView.setVisibility(View.VISIBLE);
      }
    });

As you can see, everything is correct. And as I said before the exception doesn't appear always in fact it only appeared a few times.

Does anyone have an idea of what might be causing this?

Eric Leichtenschlag

unread,
May 19, 2014, 9:10:13 PM5/19/14
to google-adm...@googlegroups.com
That's odd. The code is basically checking if (mAdView.getAdSize() == null || mAdView.getAdUnitID() == null) when loadAd happens. If you can sometimes reproduce this, maybe you could log the boolean output of (mAdView.getAdSize() == null || mAdView.getAdUnitID() == null) before calling loadAd, and see if you ever get a hit.

Adelino Lobão

unread,
May 20, 2014, 10:26:04 AM5/20/14
to google-adm...@googlegroups.com
The problem is that, I'm not able to simulate the error because it never happen when I'm developing the application.
It only happened in production a few times (in 100 requests, 1 will raise the exception), so I don't have any clue and also I'm not able to debug it.
Reply all
Reply to author
Forward
0 new messages