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.
...
if (INSTANCE.bottomAdView.getVisibility() ==</span
07-18 08:48:24.025: E/AndroidRuntime(559): FATAL EXCEPTION: main07-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)
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
AdView adView = (AdView)this.findViewById(R.id.adView);
adView.setAdUnitId("XXXXX");
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);