HarmonyOS鸿蒙Next中点击跳转会闪退;并报错Error message:Cannot read property bs of undefined???
HarmonyOS鸿蒙Next中点击跳转会闪退;并报错Error message:Cannot read property bs of undefined???
import window from '@ohos.window';
import {TabTitleBar ,TabTitleBarTabItem ,TabTitleBarMenuItem} from '@ohos.arkui.advanced.TabTitleBar';
import { AppStorageV2 } from '@kit.ArkUI';
import {baseSetting} from "../base/baseSetting"
import { Configuration } from '@kit.ArkUI';
import { ConfigurationConstant } from '@kit.AbilityKit';
import {common} from '@kit.AbilityKit'
import {geoLocationManager} from '@kit.LocationKit'
import {BusinessError} from '@kit.BasicServicesKit'
@Entry
@Component
export struct page_main {
@State bs: baseSetting = new baseSetting()
isNight: boolean = false
barColor: string = "#F5F5F5"
backColor: string = "#ffffff"
fontColor: string = "#000000"
//创建底部导航栏点击事件
@Builder
tabsbar(index:number,title:string,img:ResourceStr,selectimg:ResourceStr){
Column(){
Image(index == this.selectedindex ? selectimg : img)
.width(25)
if(index==this.selectedindex){
Text(title)
.fontColor(this.bs.fontColor)
.fontWeight(50)
}
else {
Text(title)
.fontColor(Color.Gray)
.fontWeight(30)
}
}
}
//创建实时更新变量
@State LightNight : number = 1
@State selectedindex :number =0
@State userAvatar: string | Resource = $r("app.media.morenFace")
@State userName: string = "默认用户名"
@State currentName: string = ""
@State house:string ='app.media.house_gray'
@State set: string = 'app.media.set_gray'
@State person:string='app.media.person_gray'
@State pathStack : NavPathStack | undefined = undefined
async aboutToAppear(){
this.pathStack= AppStorageV2.connect(NavPathStack, "NavPathStack", () => new NavPathStack()) ?? new NavPathStack();
}
//主函数
build() {
if (this.pathStack) {
Navigation(this.pathStack) {
Tabs({ barPosition: BarPosition.End }) {
//主页
TabContent() {
Text("主页")
.fontSize(30)
}
.backgroundColor(this.bs.backColor)
.tabBar(this.tabsbar(0, '主页', $r(this.house), $r('app.media.house')))
//设置
TabContent() {
Column() {
Text("开启悬浮窗")
Toggle({ type: ToggleType.Switch })
.onChange(() => {
this.aboutToAppear()
})
}
}
.backgroundColor(this.bs.backColor)
.tabBar(this.tabsbar(1, '设置', $r(this.set), $r('app.media.set')))
//个人中心
TabContent() {
Column() {
Row() {
Image(this.userAvatar)
.width(80)
.height(80)
.borderRadius('50%')
.objectFit(ImageFit.Cover)
.shadow({
radius: 4,
color: '#00000020',
offsetX: 0,
offsetY: 2
})
.border({ width: 2, color: '#ffffff' })
Text(this.userName)
.width("60%")
.height(40)
.fontSize(18)
.fontColor(this.bs.fontColor)
.fontWeight(FontWeight.Medium)
.margin({ top: 16 })
.backgroundColor('#00000000')
.border({ width: 0 })
Image($r("app.media.arrow_right"))
.width(20)
.height(20)
.margin({ left: 10 })
.onClick(() => {
this.pathStack?.pushPathByName("person", null, false)
})
}
.width('100%')
.justifyContent(FlexAlign.Center)
.margin({ top: 60 })
Button("切换日/夜间模式")
.onClick(() => {
this.LightNight += 1
if (Math.trunc(this.LightNight % 2) == 0) {
this.house = 'app.media.house_white'
this.person = 'app.media.person_white'
this.set = 'app.media.set_white'
} else {
this.house = 'app.media.house_gray'
this.person = 'app.media.person_gray'
this.set = 'app.media.set_gray'
}
this.bs.isNight = !this.bs.isNight
this.barColor = (this.bs.barColor = this.bs.isNight ? "#3b3f42" : "#F5F5F5")
this.backColor = (this.bs.backColor = this.bs.isNight ? "#585b5e" : "#ffffff")
this.fontColor = (this.bs.fontColor = this.bs.isNight ? "#ffffff" : "#000000")
})
.backgroundColor("#00f0c8")
更多关于HarmonyOS鸿蒙Next中点击跳转会闪退;并报错Error message:Cannot read property bs of undefined???的实战教程也可以访问 https://www.itying.com/category-93-b0.html
感谢您的提问,为了更快解决您的问题,麻烦请检查和补充以下信息:
1.请检查bs变量是否正常被初始化
2.使用上述代码测试未发现闪退,请问是点击哪个按钮发生的跳转闪退?麻烦提供一个能够复现问题的完整demo~
更多关于HarmonyOS鸿蒙Next中点击跳转会闪退;并报错Error message:Cannot read property bs of undefined???的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
先断点一步一步倒回去推测一下为什么出现undefined, 直接贴代码别人没法排查,
该错误通常是由于页面路由配置错误或目标页面未定义导致。检查页面路由配置是否正确,确保目标页面在路由表中正确定义。同时确认页面跳转代码中路径参数是否正确传递,避免出现undefined情况。可查看页面生命周期函数是否正常执行。
错误信息Cannot read property bs of undefined
表明在代码执行过程中尝试访问bs
属性时,该对象为undefined
。根据提供的代码,问题可能出现在以下位置:
-
baseSetting
实例未正确初始化:在@State bs: baseSetting = new baseSetting()
中,确保baseSetting
类已正确定义且构造函数无异常。若构造函数中存在异步操作或未处理的错误,可能导致实例化失败。 -
异步初始化问题:
aboutToAppear
生命周期函数中进行了AppStorageV2.connect
操作,但pathStack
的初始化可能未完成时,UI已开始渲染。此时若bs
尚未就绪,访问其属性会报错。 -
条件渲染依赖:在
build
函数中,this.bs.backColor
等属性被直接用于样式设置。若bs
未初始化完成,触发界面刷新(如切换Tab)时会因访问未定义属性而闪退。
建议检查:
- 确认
baseSetting
类的实现,确保构造函数无报错。 - 在访问
bs
前增加空值判断,例如:this.bs?.backColor ?? fallbackValue
。 - 验证
aboutToAppear
中pathStack
的初始化是否同步完成,必要时使用Promise
或回调确保数据就绪。
若问题仍存在,请提供baseSetting
类的具体实现以便进一步分析。