setTimeout回调方法中调用promptAction.showToast报错Internal error. UI execution context not found.

setTimeout回调方法中调用promptAction.showToast报错Internal error. UI execution context not found.

HarmonyOS
2024-10-28 09:58:48
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

​根据报错信息:Internal error. UI execution context not found.

初步推断是在调用promptAction.showToast接口时,接口识别到场景下UI实例缺失主动抛出的。请尝试绑定UI实例来调用接口,同时进行合理的try catch异常捕捉。

demo如下:​

@Entry  
@Component  
struct Index {  
  @State message: string = '从窗口实例中获取UI实例,通过UI实例使用接口';  
  aboutToAppear(): void {  
    setTimeout(() => {  
      //抓取异常  
      try {  
        this.getUIContext().getPromptAction().showToast({  
          message: '弹窗测试',  
          duration: 2000  
        })  
      } catch (e) {  
        console.error("弹窗异常,异常信息:"+JSON.stringify(e))  
      }  
    },1000)  
  }  
  
  build() {  
    Column(){  
      Text(this.message)  
        .id('HelloWorld')  
        .fontSize(15)  
        .fontWeight(FontWeight.Bold)  
        .textAlign(TextAlign.Center)  
        .width('100%')  
    }  
    .width('100%')  
  }  
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
分享
微博
QQ
微信
回复
2024-10-28 16:32:27


相关问题
HarmonyOS promptAction.showToast使用问题
741浏览 • 1回复 待解决