HarmonyOS 鸿蒙Next ets疑难杂症

HarmonyOS 鸿蒙Next ets疑难杂症 只要在foreach 方法下面写if就报错,其他电脑正常,只有我的电脑才会,这是什么插件检查问题?

@Entry
@Component
struct SecondPage {
  @State message: string = 'Hello World'
  luckyName: string[] = ["P50", "感谢老板", "平板", "现金500", "开始抽奖", "Q币40", "身体健康", "Iphone14", "谢谢参与"]

  //用于旋转下标
  @State currentIndex: number = 0
  //这是真实选中的下标
  @State selectIndex: number = 0

  build() {
    Flex({ direction: FlexDirection.Row,
      justifyContent: FlexAlign.SpaceAround,
      wrap: FlexWrap.Wrap,
      alignContent: FlexAlign.SpaceAround
    }) {
      //动态添加
      ForEach(this.luckyName, (item, index) => {

        if (index == 4) {
          //开始抽奖
          Column() {
            Button("开始抽奖")
              .height(140)
              .type(ButtonType.Normal)
              .borderRadius(20)
              .onClick(() => {
                this.startLucky()
              })
          }.width("30%")
        } else {
          //抽奖选项
          Column() {
            Image($r("app.media.icon"))
              .width(100)
              .height(100)

            Text(item)
              .fontSize(18)
              .fontWeight(FontWeight.Bold)
          }.width("30%")
          .backgroundColor(this.selectIndex == index ? Color.Pink : Color.White)
          .padding(10)
          .borderRadius(15)
        }
      })
    }.backgroundColor("#ff91f391")
    .height(500)
  }

  timerId: number = -1
  //开始抽奖函数
  startLucky() {
    //floor 向下取整
    this.stopCount = Math.floor(Math.random() * 10 + 50)
    console.info(this.stopCount + "")
    //定时执行
    //1000 毫秒
    this.timerId = setInterval(() => {
      this.countDownTimer()
    }, 80)
  }

  //当前转动的圈数
  count: number = 0
  //停止的圈数
  stopCount: number = 50
  //倒计时停止
  countDownTimer() {
    this.count++
    this.currentIndex++
    if (this.currentIndex > 7) {
      this.currentIndex = 0
    }
    this.currentIndexToSelectIndex()
    console.info(this.currentIndex + "")

    if (this.count == 20) {
      //开始减速
      //停止 以前的任务
      clearInterval(this.timerId)
      this.timerId = setInterval(() => {
        this.countDownTimer()
      }, 150)
    }

    if (this.count == this.stopCount) {
      //停止 抽奖完毕
      clearInterval(this.timerId)
      //重置
      this.count = 0
    }
  }

  //将下标进行转换
  currentIndexToSelectIndex() {
    switch (this.currentIndex) {
      case 0:
        this.selectIndex = 0;
        break
      case 1:
        this.selectIndex = 1;
        break
      case 2:
        this.selectIndex = 2;
        break
      case 3:
        this.selectIndex = 5;
        break
      case 4:
        this.selectIndex = 8;
        break
      case 5:
        this.selectIndex = 7;
        break
      case 6:
        this.selectIndex = 6;
        break
      case 7:
        this.selectIndex = 3;
        break
      case 8:
        this.selectIndex = 0;
        break
    }
  }
}

更多关于HarmonyOS 鸿蒙Next ets疑难杂症的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

开发者您好,该问题无法复现,请提供下IDE版本。

更多关于HarmonyOS 鸿蒙Next ets疑难杂症的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


是不是用的是DevEco Studio 3.0 Release版本,原来的Beta版本没有问题,升级到Release才出现的,只是飘红报错,可以正常运行。

针对帖子标题“HarmonyOS 鸿蒙Next ets疑难杂症”,以下是对可能遇到的一些问题的直接回答:

  1. 编译错误

    • 确保ets代码符合HarmonyOS的语法规范,检查是否有语法错误或遗漏的符号。
    • 确认ets.config配置文件中的路径和设置是否正确。
  2. 运行时崩溃

    • 查看日志输出,定位崩溃发生的具体位置。
    • 检查是否存在内存泄漏或越界访问等问题。
  3. UI显示异常

    • 验证布局文件是否正确加载,以及组件属性是否设置正确。
    • 检查是否有资源文件缺失或路径错误。
  4. 事件处理不响应

    • 确认事件监听器是否正确注册,并检查事件处理函数是否存在逻辑错误。
    • 验证事件触发条件是否满足。
  5. 性能问题

    • 使用性能分析工具检查应用的CPU和内存使用情况。
    • 优化代码,减少不必要的计算和内存分配。
  6. 第三方库兼容性问题

    • 确认第三方库是否支持HarmonyOS平台。
    • 查看第三方库的文档或社区,了解是否有已知的兼容性问题。

如果问题依旧没法解决请联系官网客服,官网地址是 https://www.itying.com/category-93-b0.html

回到顶部