EddyVerbruggen / nativescript-admob

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

After goBack navigation, admob broke the app #7

Open calebeaires opened 8 years ago

calebeaires commented 8 years ago

After topmost goBack, when calling the admob create banner again, I get this error

TypeError: Cannot read property 'addView' of null File: "/data/data/com.example.test/files/app/tns_modules/nativescript-admob/admob.js, line: 127, column: 55"

nativescript-admob: 1.1.4 nativescript: 2.0 emulator: android 6.0

EddyVerbruggen commented 8 years ago

Can you try fiddling with these lines to fix it?

calebeaires commented 8 years ago

Removing timeout the problem disappears!

Skintillion commented 5 years ago

I've had a problem with this plugin crashing only on certain phones and not every time.

I was able to gain a lot more stability by modifying the plugins code to only use appModule

Changed the following lines in admob.android.js from this:

if (frame_1.topmost() !== undefined) {
   frame_1.topmost().currentPage.android.getParent().addView(adViewLayout_1, relativeLayoutParamsOuter_1);
} else {
   appModule.android.foregroundActivity.getWindow().getDecorView().addView(adViewLayout_1, relativeLayoutParamsOuter_1);
}

To this:

appModule.android.foregroundActivity.getWindow().getDecorView().addView(adViewLayout_1, relativeLayoutParamsOuter_1);

After that the crashes stopped.

I even put a check for frame topmost around the firebase instantiation and that didn't help. I found that sometimes popups (permission requests) would interfere with the plugin.

If appModule works more often, why bother with the frame topmost method?

Skintillion commented 5 years ago

Ok, so if you use the appModule method, it puts the banner behind the navigation buttons.

Going back to the script....


setTimeout(function () {
  if (frame_1.topmost() !== undefined) {
    var topMost = frame_1.topmost();
    var currentPage = topMost.currentPage;
    var currentPageParent = currentPage.android.getParent();
    console.log(currentPageParent); // null
    currentPageParent.addView(adViewLayout_1, relativeLayoutParamsOuter_1);

Even though it finds frame_1.topmost(), the current page parent is null, causing an error when trying to addView.