目录

多指操作时应用出现闪退

问题现象

在多指操作页面时,应用出现闪退。

背景知识

问题定位

  1. 查看faultlogger目录下的故障日志,应用出现了JS Crash异常,故障原因是TypeError类型错误,异常信息是Cannot read property localX of undefined,无法从undefined对象中读取localX属性值。
    Reason:TypeError
    Error name:TypeError
    Error message:Cannot read property localX of undefined
    Stacktrace:
    Cannot get SourceMap info, dump raw stack:
        at anonymous (entry|entry|1.0.0|src/main/ets/components/CGBGestureUnlockView.ts:235:1)
  2. 从异常信息可以看出,应用从FingerInfo对象中读取属性localX,没有校验FingerInfo对象是否为undefined。
  3. 排查应用是否监听了手势事件,并从GestureEvent对象中获取FingerInfo对象。先按下且未参与当前手势触发的手指在fingerList中对应位置为空,在使用时必须先校验FingerInfo对象是否为空。

分析结论

在使用FingerInfo对象前未判断是否为空导致应用使用时闪退。

修改建议

在使用前判断FingerInfo对象是否为空。

@Entry
@Component
struct PanGesturePage {
  @State offsetX: number = 0;
  @State offsetY: number = 0;
  private panOption: PanGestureOptions = new PanGestureOptions({ direction: PanDirection.Left | PanDirection.Right });

  build() {
    Column() {
      Column() {
        Text('PanGesture offset:\nX: ' + this.offsetX + '\n' + 'Y: ' + this.offsetY)
      }
      .height(200)
      .width(300)
      .padding(20)
      .border({ width: 3 })
      .margin(50)
      .gesture(
        PanGesture(this.panOption)
          .onActionStart((event: GestureEvent) => {
            // 获取fingerInfos信息
            if (event?.fingerList) {
              // 使用前判断FingerInfo对象是否为空
              if (event.fingerList[0] !== undefined) {
                this.offsetX = event.fingerList[0].localX;
                this.offsetY = event.fingerList[0].localY;
              }
            }
          })
      )
    }
  }
}

上述为常规修复方案,在API20及以上版本,GestureEvent对象新增属性fingerInfos,该属性返回的是当前触摸手势的所有触摸点,不存在空值情况。