From 028071fe775eb51263303278b8fbf16b2d92b360 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=81=E6=89=AC?= Date: Tue, 9 Dec 2025 14:59:12 +0800 Subject: [PATCH] =?UTF-8?q?fix(uiContext)=EF=BC=9A=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E9=A2=84=E8=A7=88=E5=99=A8=E5=BC=B9=E7=AA=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/ets/common/utils/CommonUtils.ets | 17 +++++++++++------ .../src/main/ets/entryability/EntryAbility.ets | 1 - entry/src/main/ets/pages/Index.ets | 4 ++++ 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/entry/src/main/ets/common/utils/CommonUtils.ets b/entry/src/main/ets/common/utils/CommonUtils.ets index f955f17..56372f7 100644 --- a/entry/src/main/ets/common/utils/CommonUtils.ets +++ b/entry/src/main/ets/common/utils/CommonUtils.ets @@ -13,16 +13,21 @@ * limitations under the License. */ -import { promptAction } from '@kit.ArkUI'; import { CommonConstants } from '../constants/Constants'; -const uiContext: UIContext | undefined = AppStorage.get('uiContext'); +import Logger from './Logger'; export class CommonUtils { showToast(message: string) { - uiContext!.getPromptAction().showToast({ - message: message, - duration: CommonConstants.TOAST_TIME - }); + const uiContext: UIContext | undefined = AppStorage.get('uiContext'); + try { + uiContext!.getPromptAction().showToast({ + message: message, + duration: CommonConstants.TOAST_TIME + }); + } catch (error) { + let err = error as BusinessError; + Logger.error('CommonUtils', `Show toast failed, errCode: ${err.code}, errMessage: ${err.message}.`); + } } } diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index 99ed4d2..631333d 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -36,7 +36,6 @@ export default class EntryAbility extends UIAbility { return; } hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); - AppStorage.setOrCreate('uiContext', windowStage.getMainWindowSync().getUIContext()); }); } diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index b8c71ca..6b136b7 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -51,6 +51,10 @@ struct Index { offset: { dx: CommonConstants.OFFSET_X, dy: CommonConstants.OFFSET_Y } }); + aboutToAppear(): void { + AppStorage.setOrCreate('uiContext', this.getUIContext()); + } + build() { Column() { Text($r('app.string.ArkTS_Development_Case')) -- Gitee