HarmonyOS http请求回调后,showToast报错。?

使用http.HttpRequest.request发起http请求后,在promise的then回调里执行自定义回调函数,但是回调到页面代码后,调用toast会报错[ArkRuntime Log] Error: Internal error. UI execution context not found.

操作步骤:

1、http.createHttp();创建请求

2、request.request;发起请求

3、promise.then中处理数据然后执行回调函数

4、回调函数中使用promptAction.showToast进行输出toast

阅读 580
1 个回答

根据报错信息: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%')
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进