首页 > 解决方案 > 包含 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 模拟器运行此代码。我在这里想念什么?我是否需要在某处明确定义操作系统以防止此错误?

谢谢

标签: androidnativescript

解决方案


您必须过早访问平台模块,甚至在nativeApp实例完全初始化并准备好使用之前。

您可以等到displayed/activityCreated事件或简单地等待loaded任何组件的事件,这确保nativeApp将准备好。


推荐阅读