Anyone has Admob plugin working?



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");

        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
        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(
JS:     android.view.View.<init>(
JS:     android.view.View.<init>(
JS:     android.view.ViewGroup.<init>(
JS:     android.view.ViewGroup.<init>(
JS:     android.view.ViewGroup.<init>(
JS:     android.view.ViewGroup.<init>(
JS:<init>(Unknown Source)
JS:<init>(Unknown Source)
JS:     com.tns.Runtime.callJSMethodNative(Native Method)
JS:     com.tns.Runtime.dispatchCallJSMethodNative(
JS:     com.tns.Runtime.callJSMethodImpl(
JS:     com.tns.Runtime.callJSMethod(
JS:     com.tns.Runtime.callJSMethod(
JS:     com.tns.Runtime.callJSMethod(
JS:     com.tns.NativeScriptActivity.onStart(
JS:     android.os.Handler.dispatchMessage(
JS:     android.os.Looper.loop(
JS:     java.lang.reflect.Method.invoke(Native Method)

What am I doing wrong?


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.


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


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