跳转到新页面时,应用出现闪退现象。
Reason:TypeError
Error name:TypeError
Error message:Cannot load property of null or undefined
Stacktrace:
at anonymous entry (entry/src/main/ets/pages/TestLoadThirdPage.ets:17:25)
at initialRender entry (entry/src/main/ets/pages/TestLoadThirdPage.ets:27:18)
at pushUrl (.../foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/jsUIContext_ohos.js:1167:1)
at anonymous entry (entry/src/main/ets/pages/TestLoadPage.ets:11:43) class ParamInfo {
name: string = '';
constructor(name: string) {
this.name = name;
}
}
@Entry
@Component
struct TestLoadSecondPage {
@State message: ParamInfo = this.getUIContext().getRouter().getParams() as ParamInfo;
build() {
RelativeContainer() {
Text(this.message.name)
.id('LoadUndefinedSecondPageHelloWorld')
.fontSize($r('app.float.page_text_font_size'))
.fontWeight(FontWeight.Bold)
.alignRules({
center: { anchor: '__container__', align: VerticalAlign.Center },
middle: { anchor: '__container__', align: HorizontalAlign.Center }
});
}
.height('100%')
.width('100%');
};
} 传递的参数为空导致应用闪退。
在使用getParams获取传递的参数时需要校验是否为空。
class ParamInfo {
name: string = '';
constructor(name: string) {
this.name = name;
}
}
@Entry
@Component
struct TestLoadThirdPage {
@State message: ParamInfo = this.getUIContext().getRouter().getParams() as ParamInfo ?
(this.getUIContext().getRouter().getParams() as ParamInfo) : new ParamInfo('Two2');
build() {
RelativeContainer() {
Text(this.message.name)
.id('LoadUndefinedThirdPageHelloWorld')
.fontSize($r('app.float.page_text_font_size'))
.fontWeight(FontWeight.Bold)
.alignRules({
center: { anchor: '__container__', align: VerticalAlign.Center },
middle: { anchor: '__container__', align: HorizontalAlign.Center }
});
}
.height('100%')
.width('100%');
}
}
合作咨询
我们的专家服务团队将竭诚为您提供专业的合作咨询服务
解决方案
精准高效的一站式服务支持,助力开发者商业成功