【问题标题】:admob with nativescript-vue integrationadmob 与 nativescript-vue 集成
【发布时间】:2019-02-08 08:55:33
【问题描述】:

我正在尝试使用“nativescript-admob”将 admob 添加到我的应用程序中,这是我现在拥有的代码:

<template>
  <Page class="page">
   <StackLayout class="hello-world">
    <Label text="my home page"/>
  </StackLayout>
 </Page>
</template> 

<script>
const admob = require("nativescript-admob");
export default  {   
  mounted() {
    admob.createBanner({
   // if this 'view' property is not set, the banner is overlayed on the current top most view
   // view: ..,
   testing: true, // set to false to get real banners
   size: admob.AD_SIZE.SMART_BANNER, // anything in admob.AD_SIZE, like admob.AD_SIZE.SMART_BANNER
   androidBannerId: "ca-app-pub-AAAAAAAAAAA/AAAAAAAAA", // 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);
    }
 )
},

}
</script>

我尝试在“已安装”上启动 admob,但我无法使其工作,是否有人以这种方式将 admob 与 nativescript-vue 集成?在我的情况下,我什至没有看到日志“admob createBanner done”日志,所以也许我没有很好地使用这个插件。

【问题讨论】:

  • 请执行npm run clean,然后再试一次。
  • 我做到了,但我仍然有一个错误:'admob createBanner error: Error: java.lang.NullPointerException: Attempt to invoke virtual method \'android.content.res.Resources android.content.Context .getResources()\' 在空对象引用上
  • 如果您执行tns debug,您将看到以下错误:'Error in admob.createBanner: TypeError: null is not an object (evaluating 'utils.ios.getter(UIApplication, UIApplication.sharedApplication).keyWindow.rootViewController')'

标签: vuejs2 admob nativescript nativescript-vue


【解决方案1】:

admob.createBanner() 函数必须被 setTimeout() 包围才能启动,它似乎必须在页面完全加载时启动。感谢 slack nativescript-vue 频道贡献者的帮助!

【讨论】:

  • 如果只需要在页面加载时加载,您可以在页面标签中添加@loaded="yourFunction",它会在页面元素加载时触发 yourFunction。您应该能够在该函数中运行 admob.createBanner() 代码。如果它有效,它比依赖 setTimeout() 好一点,因为加载元素的时间可能因不同设备而异。
猜你喜欢
  • 2020-06-06
  • 1970-01-01
  • 1970-01-01
  • 2020-08-15
  • 2015-08-11
  • 1970-01-01
  • 1970-01-01
  • 2020-12-10
  • 1970-01-01
相关资源
最近更新 更多