android - 包含 platform.js 后,Nativescript 应用程序无法构建
问题描述
我有一个需要访问平台信息(设备、屏幕)的 Nativscript 5.0 应用程序。我添加了 tns 核心模块 platform.js,然后我用“tns run android”重建了整个项目。
现在我收到此错误:
System.err: TypeError: Cannot read property 'getContentResolver' of undefined
tns_modules/tns-core-modules/platform/platform.js', line: 79, column: 83
这个错误是指platform.android.js中的这段代码:
Object.defineProperty(Device.prototype, "uuid", {
get: function () {
if (!this._uuid) {
var nativeApp = appModule.android.nativeApp;
this._uuid = android.provider.Settings.Secure.getString(nativeApp.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
}
return this._uuid;
},
enumerable: true,
configurable: true
});
具体来说,nativeApp 似乎是未定义的。我正在通过 Android 模拟器运行此代码。我在这里想念什么?我是否需要在某处明确定义操作系统以防止此错误?
谢谢
解决方案
您必须过早访问平台模块,甚至在nativeApp
实例完全初始化并准备好使用之前。
您可以等到displayed
/activityCreated
事件或简单地等待loaded
任何组件的事件,这确保nativeApp
将准备好。
推荐阅读
- excel - 如何在excel中合并数据行
- apache-spark - 使用普罗米修斯操作员火花提交
- debugging - GoLand IDE:由 shell 脚本启动的调试程序(Kubernetes 本地集群)
- python - 在按键事件函数内访问/分配全局变量
- javascript - 为什么粗体标签之间的文本不呈现粗体?
- c - GCC:未定义的溢出行为是否应该保持逻辑一致性?
- mysql - 何时使用浮点数与小数
- java - Hazelcast:如何保证 EventListener 不会错过任何事件?
- r - 如何识别数据框中相对于其他数据框的唯一列?
- python - pypyodbc 与 Filemaker 的连接引发“列表索引超出范围”错误