I managed to integrate Admob correctly with my cocos2dx project. The ad displays fine on initialization from java code.
I then made 2 functions in java showAd() and hideAd() which basically just calls adView.SetVisibility() function to show/hide the Admob ad.
Now the problem begins…
I call showAd from jni, the call throws an exception (just says null) and crashes my app (I’m calling the jni function properly). But when I call the same function from java, it executes properly, thus showing the ad. I’m not sure where the problem is? Please help.
Below is my Initialize call for setting up Admob
public void initAdmobAds() { try{ LinearLayout.LayoutParams adParams = new LinearLayout.LayoutParams( 1024, 768-110); adView = new AdView(abcapp.this, AdSize.BANNER, "a15125246095824"); adView.loadAd(new AdRequest()); // Adding full screen container addContentView(adView, adParams); adView.setVisibility(AdView.INVISIBLE); }catch (Exception e) { System.out.print("ADMOB------>ADMOB error (init): "); System.out.println(e.getMessage()); } }
Now the 2 functions to show & hide the Admob adView
ok, found the problem. I was calling the showAd() function on another thread instead of the main thread. When you call from JNI, you need to ensure you get the context of your activity and then make sure you are calling your function on your main thread.
So I’ve modified my code below to make sure I run on my main thread. So it works!
public void showadspopup() { try{ ((Activity)mContext).runOnUiThread(new Runnable() {Override
public void run() {
adView.setVisibility(AdView.VISIBLE);
}
});
Hi Srini, can you give me a detail, or even your complete onCreate method? and the static java method that called by cocos?
I see you call static method from cocos “env->Call*Static*VoidMethod(mClass,method);”
But in Java, you declare your the method as “public void showadspopup” -without static
It will cause error on me because the cocos not found the static function.
And, I can’t get the adView reference from static method.
Does anyone know how to solve it?
Thanks @christian : I’ve been trying your code. But i don’t understand what theGameActivity me is. I’ve try to use my main class that extends the CocosActivity and implements AdListener right? And then you assign it, and doing nothing with it to the rest of your code. Mine is compiled without error, but then it will cause force close.