Anyone has Admob plugin working?


#1

Hi!

I’m trying to get the admob plugin to work, but it seems to behave correctly only on it’s demo app.

Here’s what I do:

  • tns create admob-test --template tns-template-blank
  • tns plugin add nativescript-admob

And then in the home-page.js I add the code:

function onNavigatingTo(args) {
    const page = args.object;
    page.bindingContext = homeViewModel;
    var admob = require("nativescript-admob");

    admob.createBanner({
        testing: true, // set to false to get real banners
        size: admob.AD_SIZE.SMART_BANNER,
        //iosBannerId: "ca-app-pub-XXXXXX/YYYYYY", // add your own
        androidBannerId: "ca-app-pub-9517346003011652/7749101329", // add your own
        margins: {
            // if both are set, top wins
            //top: 10
            bottom: 50
        }
    }).then(
        function () {
            console.log("admob createBanner done");
        },
        function (error) {
            console.log("admob createBanner error: " + error);
        }
        );
}
exports.onNavigatingTo = onNavigatingTo;

I don’t think banner ID is important in the test mode, is it?

But all I get is errors:

JS: admob createBanner error: Error: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
JS:     android.view.ViewConfiguration.get(ViewConfiguration.java:364)
JS:     android.view.View.<init>(View.java:4022)
JS:     android.view.View.<init>(View.java:4146)
JS:     android.view.ViewGroup.<init>(ViewGroup.java:579)
JS:     android.view.ViewGroup.<init>(ViewGroup.java:575)
JS:     android.view.ViewGroup.<init>(ViewGroup.java:571)
JS:     android.view.ViewGroup.<init>(ViewGroup.java:567)
JS:     com.google.android.gms.ads.BaseAdView.<init>(Unknown Source)
JS:     com.google.android.gms.ads.AdView.<init>(Unknown Source)
JS:     com.tns.Runtime.callJSMethodNative(Native Method)
JS:     com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1088)
JS:     com.tns.Runtime.callJSMethodImpl(Runtime.java:970)
JS:     com.tns.Runtime.callJSMethod(Runtime.java:957)
JS:     com.tns.Runtime.callJSMethod(Runtime.java:941)
JS:     com.tns.Runtime.callJSMethod(Runtime.java:933)
JS:     com.tns.NativeScriptActivity.onStart(NativeScriptActivity.java:38)
JS:     android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1248)
JS:     android.app.Activity.performStart(Activity.java:6696)
JS:     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2628)
JS:     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
JS:     android.app.ActivityThread.-wrap12(ActivityThread.java)
JS:     android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
JS:     android.os.Handler.dispatchMessage(Handler.java:102)
JS:     android.os.Looper.loop(Looper.java:154)
JS:     android.app.ActivityThread.main(ActivityThread.java:6119)
JS:     java.lang.reflect.Method.invoke(Native Method)
JS:     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
JS:     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

What am I doing wrong?


#2

I think you’re calling it too soon (before the view is ready). As an experiment wrap createBanner in a setTimeout of 2000 ms - if that works look for a nicer solution.


#3

Thanks! That’s it :slight_smile: {NS} is awesome :slight_smile:


#4

Hello pls share me ur code for home.js , home.xml and home-view-model.js on Admob project.