diff --git a/entry/src/main/ets/pages/LauncherPage.ets b/entry/src/main/ets/pages/LauncherPage.ets index 8dde1aae6ef8de0e8d5b9389e4cbfe205af48455..8fb29c2c8058ec54aaeece330bba70a990848d4e 100644 --- a/entry/src/main/ets/pages/LauncherPage.ets +++ b/entry/src/main/ets/pages/LauncherPage.ets @@ -20,6 +20,8 @@ import Logger from '../common/utils/Logger'; import CommonConstants from '../common/constants/CommonConstants'; import CustomDialogComponent from '../view/CustomDialogComponent'; import { GlobalContext } from '../common/utils/GlobalContext'; +import { BusinessError } from '@kit.BasicServicesKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; /** * The LauncherPage is the entry point of the application and shows how to develop the LauncherPage. @@ -50,7 +52,9 @@ struct LauncherPage { onCancel() { // Exit the application. - this.context?.terminateSelf(); + this.context?.terminateSelf().catch((err: BusinessError) => { + hilog.error(0x0000, 'LauncherPage', `terminateSelf failed. code=${err.code}, message=${err.message}`); + }); } onConfirm() { @@ -83,8 +87,11 @@ struct LauncherPage { } saveIsPrivacy() { - let preferences: Promise = this.getDataPreferences(this); - preferences.then((result: preferences.Preferences) => { + let preferences: Promise = this.getDataPreferences(this); + preferences.then((result: preferences.Preferences | void) => { + if (!result) { + return; + } let privacyPut = result.put(CommonConstants.PREFERENCES_KEY_PRIVACY, false); result.flush(); privacyPut.then(() => { @@ -100,7 +107,10 @@ struct LauncherPage { onPageShow() { this.context = this.getUIContext().getHostContext() as common.UIAbilityContext; // Get the operation class for saving data. - this.getDataPreferences(this).then((preferences: preferences.Preferences) => { + this.getDataPreferences(this).then((preferences: preferences.Preferences | void) => { + if (!preferences) { + return; + } preferences.get(CommonConstants.PREFERENCES_KEY_PRIVACY, true).then((value: preferences.ValueType) => { Logger.info(CommonConstants.LAUNCHER_PAGE_TAG, 'onPageShow value: ' + value); if (value) { @@ -113,14 +123,17 @@ struct LauncherPage { this.jumpToAdvertisingPage(); } }); - }); + }) } /** * Get data preferences action. */ getDataPreferences(common: Object) { - return preferences.getPreferences(this.getUIContext().getHostContext()!, CommonConstants.PREFERENCES_FILE_NAME); + return preferences.getPreferences(this.getUIContext().getHostContext()!, CommonConstants.PREFERENCES_FILE_NAME) + .catch((err: BusinessError) => { + hilog.error(0x0000, 'LauncherPage', `getPreferences failed. code=${err.code}, message=${err.message}`); + }); } build() {