EddyVerbruggen / nativescript-admob

NativeScript plugin to earn some precious :moneybag::moneybag: with ads by Google AdMob
MIT License
69 stars 26 forks source link

TypeError: Cannot read property 'android' of null #36

Closed Spahar closed 6 years ago

Spahar commented 6 years ago

Hello, I am encountering this Exception. The thing is that it doesn't happen every time.

Error:

JS:  *** NativeScriptError *** : Error: The application crashed because of an uncaught exception. You can look at "stackTrace" or "nativeException" for more detailed information about the exception.
JS:  *** StackTrace *** : An uncaught Exception occurred on "main" thread.
JS: com.tns.NativeScriptException:
JS: Calling js method run failed
JS:
JS: TypeError: Cannot read property 'android' of null
JS: File: "file:///data/data/com.antibooker.onthego/files/app/tns_modules/nativescript-admob/admob.js, line: 141, column: 35
JS:
JS: StackTrace:
JS:     Frame: function:'', file:'file:///data/data/com.antibooker.onthego/files/app/tns_modules/nativescript-admob/admob.js', line: 141, column: 36
JS:     Frame: function:'invoke', file:'file:///data/data/com.antibooker.onthego/files/app/tns_modules/tns-core-modules/timer/timer.js', line: 19, column: 48
JS:     Frame: function:'run', file:'file:///data/data/com.antibooker.onthego/files/app/tns_modules/tns-core-modules/timer/timer.js', line: 23, column: 13
JS:
JS:
JS:     at com.tns.Runtime.callJSMethodNative(Native Method)
JS:     at com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1088)
JS:     at com.tns.Runtime.callJSMethodImpl(Runtime.java:970)
JS:     at com.tns.Runtime.callJSMethod(Runtime.java:957)
JS:     at com.tns.Runtime.callJSMethod(Runtime.java:941)
JS:     at com.tns.Runtime.callJSMethod(Runtime.java:933)
JS:     at com.tns.gen.java.lang.Runnable.run(Runnable.java:10)
JS:     at android.os.Handler.handleCallback(Handler.java:739)
JS:     at android.os.Handler.dispatchMessage(Handler.java:95)
JS:     at android.os.Looper.loop(Looper.java:148)
JS:     at android.app.ActivityThread.main(ActivityThread.java:5417)
JS:     at java.lang.reflect.Method.invoke(Native Method)
JS:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
JS:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
JS:  *** nativeException *** : com.tns.NativeScriptException:

I started developing the app using the tabs example.

The createBanner is called through the tabs-page.js

function onLoaded () {
    setTimeout( function() {
        admob.createBanner({
          testing: true,
          size: admob.AD_SIZE.SMART_BANNER,
          androidBannerId: "ca-app-pub-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
          margins: {
            bottom: 0
          },
          keywords: ["foo", "bar"]
        }).then(
            function() {
              console.log("admob createBanner done");
            },
            function(error) {
              console.log("admob createBanner error: " + error);
            }
        );
    }, 1500);
}

exports.onLoaded = onLoaded;

Any suggestions?

Thanks

redabelca commented 6 years ago

increase setTimeout Time from 1500 to 4000 for example and try

Spahar commented 6 years ago

First of all, thanks for the reply.

I have thought of that, but I don't believe this is a permanent solution. Because sometime for some reason, even 4 seconds won't be enough.

Apart from finding a fix, is there a way to suppress the exception so it wont crash the app?

Spahar commented 6 years ago

This fix proposed from RoyiNamir here seems to do the trick.

EddyVerbruggen commented 6 years ago

Applied the fix, cheers!