HarmonyOS鸿蒙Next中arkui-x跨平台安卓真机运行闪退报错:TypeError: is not callable 怎么解决

HarmonyOS鸿蒙Next中arkui-x跨平台安卓真机运行闪退报错:TypeError: is not callable 怎么解决 在安卓真机上运行,点击应用中某功能按钮后,真机闪退报错:

cke_171.png

代码如下,注释了大部分代码后,在Android Studio中真机运行输出结果如上图

import {WordViewModel,Example,Phrases,Phrase} from '../viewModel/WordViewModel'
import {AudioView, CollectView,ConfirmView,SpellView} from '../views/SpellButtonsView'
import {CenterTitleView} from '../views/CenterTitleView'
import { LeftTitleView } from '../views/LeftTitleView'
import { SpellPracticeApi } from '../api/SpellPracticeApi'
import { ToastUtils } from '../utils/ToastUtils'
import { PromptAction } from '@kit.ArkUI';
import { DailyTask } from '../viewModel/DailyTaskVewModel'
import { TransFormUtils } from '../utils/TransformUtils'

@ComponentV2
export struct SpellPractisePage{

  private uiContext: UIContext = this.getUIContext();
  private promptAction: PromptAction = this.uiContext.getPromptAction();

  @Consumer('pageStack') pageStack:NavPathStack= new NavPathStack()

  @Provider('word') word:WordViewModel=new WordViewModel()
  @Provider('input') input:string=''
  @Provider('isShown') isShown:boolean=false
  @Provider('isCorrectThisTime') isCorrect:boolean=false

  @Local currentWords:WordViewModel[]=[]
  @Local nextWords:WordViewModel[]=[]

  //@Consumer('dailyTask') dailyTask:DailyTask= new DailyTask() // todo 暂时替代要练习的单词数组
  @Local currentIndex:number=0

  @Local done:boolean=false
  @Local pageActive:boolean=true


  // 每次点击拼写练习都会请求新的数组
/*  async getSpellPracticeWords(){
    try {
      this.done=false

      const response= await SpellPracticeApi.getSpellPracticeWords()

      if(this.currentWords.length===0){
        this.currentWords=TransFormUtils.transferInter_To_WordModelArr(response.data.data)
      }else {
        this.nextWords=TransFormUtils.transferInter_To_WordModelArr(response.data.data)
      }


      console.log(`daisyS getSpellPracticeWords successfully, this.wordsToSpell:${JSON.stringify(this.currentWords)}`)
    }catch (e) {
      console.log(`daisyS getSpellPracticeWords failed, error msg:${e.message}`)
      ToastUtils.showToast(this.promptAction,'单词获取失败,请检查网络设置','getSpellPracticeWords',this.pageStack)
    }finally {
      this.done=true
    }
  }
*/
 /* aboutToAppear(): void {
    this.getSpellPracticeWords()
    //this.word=this.dailyTask.waitToLearn[this.currentIndex]
    if(this.currentWords.length){
      this.word=this.currentWords[this.currentIndex]
    }
  }*/
/*
  async postSingleWordSpellRecord(){
    try {
      this.done=false
      const data:SpellPracticeApi.WordData=TransFormUtils.transferWordModel_To_Inter(this.word)
      const response= await SpellPracticeApi.postSingleWordSpellRecord(data)

      console.log(`daisyS postSingleWordSpellRecord successfully, data:${data}`)
    }catch (e) {
      console.log(`daisyS postSingleWordSpellRecord failed, error msg:${e.message}`)
      ToastUtils.showToast(this.promptAction,'单词信息同步失败,已记录本地','postSingleWordSpellRecord') // todo 加入同步队列
    }finally {
      this.done=true
    }
  }

  @Monitor('isCorrect')
  onIsCorrectChange(monitor:IMonitor){
    if(this.isCorrect){

      setTimeout(()=>{
        this.isCorrect=false
        this.input=''
        this.pageStack.pushPathByName('ExplanationPage',this.word,(popInfo)=>{
          console.log(`song I am in callback popInfo of SpellPracticePage ,popInfo.result:${JSON.stringify(popInfo.result as WordViewModel)}`)
          this.word=popInfo.result as WordViewModel

          //if(this.currentIndex<this.dailyTask.waitToLearn.length-1){
            //this.currentIndex++
            //this.word=this.dailyTask.waitToLearn[this.currentIndex]
          //}
          this.postSingleWordSpellRecord()

          if(this.currentIndex<this.currentWords.length-1){
            this.currentIndex++

            if(this.currentWords.length-1-this.currentIndex===5){
              this.getSpellPracticeWords() // 获取预加载数组
            }
          }else { // 当前数组的最后一个已经拼写完成
            this.currentIndex=0
            this.currentWords=this.nextWords //替换
          }

          this.word=this.currentWords[this.currentIndex]

        })// 把当前单词传给解释界面,准备解释
      },800)

    }
  }

*/
  build() {
    NavDestination(){
/*
      Column(){
        LeftTitleView({title:'拼写练习'}).margin({top:20,left:20})



        SpellView().margin({top:130})


        Blank()

        Row(){
          CollectView({pageActive:this.pageActive})
          AudioView()
          ConfirmView()
        }.width('100%')
        .justifyContent(FlexAlign.SpaceEvenly)
        .margin({top:100})

      }.height('65%')//.backgroundColor(Color.Pink)
      .expandSafeArea([SafeAreaType.KEYBOARD],[SafeAreaEdge.BOTTOM])
*/
    }.hideTitleBar(true)
    .hideBackButton(true)
    .hideToolBar(true)
    .onActive(()=>{
      this.pageActive=true
    })
    .onHidden(()=>{
      this.pageActive=false
    })
  }
}

更多关于HarmonyOS鸿蒙Next中arkui-x跨平台安卓真机运行闪退报错:TypeError: is not callable 怎么解决的实战教程也可以访问 https://www.itying.com/category-93-b0.html

4 回复

【解决方案】

ArkUI-X作为基于ArkUI的跨平台开发框架,提供了完整的应用开发指导文档,如下:

  • 快速开始主要包含跨平台应用快速入门、开发基础知识,帮助开发者快速了解跨平台应用开发的基础流程。
  • 环境搭建帮助开发者搭建ArkUI-X的开发环境。
  • API参考提供了ArkUI-X支持的组件列表、接口参考文档和平台集成接口说明,可以帮助开发者快速查找到支持的指定接口的详细描述和调用方法。
  • 平台桥接平台差异化提供了针对不同平台间的差异的适配方法。
  • Android平台开发提供了Android平台和ArkUI-X混合开发指导。
  • iOS平台开发提供了iOS平台和ArkUI-X混合开发指导。
  • 动态化能力允许开发者根据需要动态发布跨平台内容,从而使跨平台部分和宿主应用进行解耦。动态化能力包括但不限于动态加载UI组件、数据和业务逻辑,适用于需要频繁更新的应用场景。
  • 单元/UI测试提供了跨平台测试开发指导。
  • 示例工程提供了一系列的应用示例,每一个应用示例都是一个独立的跨平台应用工程项目,开发者可以使用DevEco Studio和Ace Tools命令行开发工具浏览代码、编译工程、安装和运行应用示例来了解ArkUI跨平台应用开发流程。
  • ArkUI-X版本提供了当前已发布的版本详细信息说明。

https://gitcode.com/arkui-x/docs/blob/master/zh-cn/application-dev/reference/arkui-ts/ts-basic-components-navdestination.md

navdestination组件组件除了通用事件外,支持以下事件:

onShown(callback: () => void)10+:当该NavDestination页面显示时触发此回调。onHidden(callback: () => void)10+:当该NavDestination页面隐藏时触发此回调。onBackPressed(callback: () => boolean)10+:当点击返回键时,触发该回调。返回值为true时,表示重写返回键逻辑,返回值为false时,表示回退到上一个页面。

更多关于HarmonyOS鸿蒙Next中arkui-x跨平台安卓真机运行闪退报错:TypeError: is not callable 怎么解决的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


是写了onActive和onHidden,我把这两个注释掉就不会闪退了,但是为什么啊

该错误通常由ArkUI-X跨平台组件调用方式错误导致。检查代码中是否存在将非函数类型作为函数调用的语句,特别是涉及安卓平台特定API时。确认所有被调用的方法都已正确定义且未被意外重写为其他类型。建议使用DevEco Studio的日志功能定位具体出错位置,核对类型定义与调用方式是否匹配。

从错误信息和代码来看,TypeError: is not callable通常是因为尝试调用了一个非函数类型的变量或属性。在您的代码中,最可能的原因是PromptAction的使用方式不正确。

在HarmonyOS Next中,PromptAction应该通过getContext(this)获取,而不是通过UIContext.getPromptAction()。请修改以下代码:

private promptAction: PromptAction = getContext(this).getPromptAction();

同时检查ToastUtils.showToast()方法的调用方式,确保传递的参数类型与PromptAction的方法签名匹配。如果问题仍然存在,建议检查所有被调用的方法是否正确定义为函数类型。

回到顶部