I’m trying to use C++ to call a Java method I made called void showAd() that loads a banner ad from Admob (adView.loadAd(adRequest)). The showAd() method is in the SDLActivity class in SDLActivity.java and the adView and adRequest are public SDLActivity class level variables.
In onCreate() I initialize mobile ads, create new AdView(context: this), set banner type, set test ID, build an AdRequest, and add the adView the view hierarchy so it’s visible.
My C++ code calls the Java function perfectly using JNI. I know this because if I remove adView.load(adRequest) from showAd() and replace with generic output like Log.v(“test”) it outputs to LogCat percetly and returns the execution flow back to the C++ program.
If I simply move adView.load(adRequest) to the OnCreate() method, it works fine, however I lose the ability to control the adView through C++ (reloading, hiding, showing, etc.)
I’ve tried static and non-static versions of the adView, adRequest variables and showAd() method with no success.
Any guidance is appreciated. Below is some code:
SDLActivity.java
public class SDLActivity extends Activity
{
…
protected void onCreate(Bundle savedInstanceState)
{
…
public AdRequest adRequest;
…
MobileAds.initialize(this, “ca-app-pub-3940256099942544~3347511713”);
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(“ca-app-pub-3940256099942544/6300978111”);
adRequest = new AdRequest.Builder().build();
mLayout.addView( adView );
…
} // end onCreate()
public void showAd() {
adView.loadAd( adRequest );
}
} // end SDLActivity class
C++ using SDL & JNI to call Java method (works perfectly):
{
JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();
jobject activity = (jobject)SDL_AndroidGetActivity();
jclass clazz(env->GetObjectClass(activity));
jmethodID method_id = env->GetMethodID(clazz, “showAd”, “()V”);
env->CallVoidMethod(activity, method_id);
}