Change Ad Unit ID of AdView

1,396 views
Skip to first unread message

Dat Nguyen

unread,
Jul 16, 2014, 2:20:08 AM7/16/14
to google-adm...@googlegroups.com
Could I use many Ad Unit Ids for the same AdView? I want to change Ad Unit Id at each screen. On iOS, it doesn't have any problem, but on Android, it goes crashed.

Thank you in advance.

Amy Quispe (AdMob SDK Team)

unread,
Jul 16, 2014, 6:42:26 PM7/16/14
to google-adm...@googlegroups.com
Hi Dat,

You should definitely be able to use different Ad Units for the same AdView on Android. Could you show me your code?

Amy Quispe

Dat Nguyen

unread,
Jul 16, 2014, 10:27:34 PM7/16/14
to google-adm...@googlegroups.com
Yes. Here's the code:

public static void setAdUnitID(String id)
{
     INSTANCE.adUnitID = id;
}

public static void initializeAdMob(String id)
{
     INSTANCE.adUnitID = id;
     INSTANCE.bottomAdView = new AdView(INSTANCE.mCurrentActivity);
     INSTANCE.bottomAdView.setAdSize(AdSize.BANNER);
     INSTANCE.bottomAdView.setAdUnitId(INSTANCE.adUnitID);
     INSTANCE.bottomAdView.setAdListener(INSTANCE);
}

public static void loadAd()
{
     final LinearLayout.LayoutParams adParams = new LinearLayout.LayoutParams(
         LinearLayout.LayoutParams.WRAP_CONTENT,
         LinearLayout.LayoutParams.WRAP_CONTENT);

     final AdRequest adRequest = new AdRequest.Builder()
     .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
    .addTestDevice("xxxxxxxxxxxxxxxxxxxxxxx")
    .build();

     INSTANCE.mCurrentActivity.runOnUiThread(new Runnable()
     {
          @Override
         public void run()
         {
              INSTANCE.bottomAdView.setAdUnitId(INSTANCE.adUnitID);

              INSTANCE.bottomAdView.loadAd(adRequest);
              INSTANCE.bottomAdView.setBackgroundColor(Color.BLACK);
              INSTANCE.bottomAdView.setBackgroundColor(0);
              INSTANCE.mCurrentActivity.addContentView(INSTANCE.bottomAdView, adParams);
         }
    });
}

public static void showAd()
{
     INSTANCE.mCurrentActivity.runOnUiThread(new Runnable()
     {
          @Override
         public void run()
         {
              if (!INSTANCE.bottomAdView.isEnabled())
                   INSTANCE.bottomAdView.setEnabled(true);

              if (INSTANCE.bottomAdView.getVisibility() == 4 )
                   INSTANCE.bottomAdView.setVisibility(View.VISIBLE);
         }
    });
}


And app crashed with error log:

07-17 09:21:04.321: E/AndroidRuntime(12381): FATAL EXCEPTION: main
07-17 09:21:04.321: E/AndroidRuntime(12381): java.lang.IllegalStateException: The ad unit ID can only be set once on AdView.


William Ferguson

unread,
Jul 17, 2014, 8:24:18 AM7/17/14
to google-adm...@googlegroups.com
Dat, you really want to change those static methods to non-static and remove that presumably static INSTANCE attribute. Statics will bite you badly, it is also very non object oriented.


William

              if (INSTANCE.bottomAdView.getVisibility() ==</span
...

Amy Quispe (AdMob SDK Team)

unread,
Jul 17, 2014, 7:51:56 PM7/17/14
to google-adm...@googlegroups.com
Dat - Can I see logs? Where is it crashing?

Dat Nguyen

unread,
Jul 17, 2014, 10:00:46 PM7/17/14
to google-adm...@googlegroups.com
Thank you guys.

Because of using JNI so I often use static methods. It's no problem if I set Ad Unit Id once on Adview, but I want to change it frequently. It crashed right after I call setAdUnitId()
method in loadAd(). Here's the log:

07-18 08:48:24.025: E/AndroidRuntime(559): FATAL EXCEPTION: main
07-18 08:48:24.025: E/AndroidRuntime(559): java.lang.IllegalStateException: The ad unit ID can only be set once on AdView.
07-18 08:48:24.025: E/AndroidRuntime(559): at com.google.android.gms.internal.at.setAdUnitId(Unknown Source)
07-18 08:48:24.025: E/AndroidRuntime(559): at com.google.android.gms.ads.AdView.setAdUnitId(Unknown Source)
07-18 08:48:24.025: E/AndroidRuntime(559): at org.cocos2dx.cpp.AdHelper$2.run(AdHelper.java:164)
07-18 08:48:24.025: E/AndroidRuntime(559): at android.os.Handler.handleCallback(Handler.java:605)
07-18 08:48:24.025: E/AndroidRuntime(559): at android.os.Handler.dispatchMessage(Handler.java:92)
07-18 08:48:24.025: E/AndroidRuntime(559): at android.os.Looper.loop(Looper.java:137)
07-18 08:48:24.025: E/AndroidRuntime(559): at android.app.ActivityThread.main(ActivityThread.java:4514)
07-18 08:48:24.025: E/AndroidRuntime(559): at java.lang.reflect.Method.invokeNative(Native Method)
07-18 08:48:24.025: E/AndroidRuntime(559): at java.lang.reflect.Method.invoke(Method.java:511)
07-18 08:48:24.025: E/AndroidRuntime(559): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
07-18 08:48:24.025: E/AndroidRuntime(559): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
07-18 08:48:24.025: E/AndroidRuntime(559): at dalvik.system.NativeStart.main(Native Method)

Amy Quispe (AdMob SDK Team)

unread,
Jul 18, 2014, 6:43:51 PM7/18/14
to google-adm...@googlegroups.com
Hi Dat,

Try creating destroying the old AdView and creating a new one (in the same place) when you want to reset the Ad Unit.

Amy Quispe

Dat Nguyen

unread,
Jul 21, 2014, 9:59:41 PM7/21/14
to google-adm...@googlegroups.com
Thank you Amy. It seems that I have to destroy and create a new one. :( really don't expect this.

Erol TOKALACOGLU

unread,
Apr 14, 2015, 2:58:34 AM4/14/15
to google-adm...@googlegroups.com
Hi Amy,

Could you please share a part of code to solve this problem?
What do you mean with destroying and creating an AdView? Without changing the ad unit ID, the ads are serving normally.  But I need to use different ad units in my application, and I can't change the ad unit id et al.

04-14 09:34:15.985: E/AndroidRuntime(17844): Caused by: java.lang.IllegalStateException: The ad unit ID can only be set once on AdView.
04-14 09:34:15.985: E/AndroidRuntime(17844): at com.google.android.gms.internal.bh.setAdUnitId(Unknown Source)
04-14 09:34:15.985: E/AndroidRuntime(17844): at com.google.android.gms.ads.AdView.setAdUnitId(Unknown Source)
04-14 09:34:15.985: E/AndroidRuntime(17844): at com.crenno.teknoblog.haberler.HaberlerActivity.onCreate(HaberlerActivity.java:96)
04-14 09:34:15.985: E/AndroidRuntime(17844): at android.app.Activity.performCreate(Activity.java:5372)
04-14 09:34:15.985: E/AndroidRuntime(17844): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
04-14 09:34:15.985: E/AndroidRuntime(17844): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)

if I use 

AdView adView = new AdView(this);

        adView.setAdUnitId("ca-app-pub-xxxx");

        adView.setAdSize(AdSize.SMART_BANNER);

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

        adView.loadAd(adRequest);

Than 

Ad is not visible, not refreshing ad

If I use;

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

        adView.setAdUnitId("XXXXX");

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

        adView.loadAd(adRequest);

I got the error above..

Thank you for your time and consideration..

Regards..

19 Temmuz 2014 Cumartesi 01:43:51 UTC+3 tarihinde Amy Quispe (AdMob SDK Team) yazdı:

Ram Parameswaran (Mobile SDK Team)

unread,
Apr 14, 2015, 1:50:50 PM4/14/15
to google-adm...@googlegroups.com
You will not be able to change the adUnit for an adView. You can initialize and reference a new adView. The old adView should be garbage collected once it loses all references.

- Ram
Reply all
Reply to author
Forward
0 new messages