I can not open an alert/confirm dialog by program, such as invoking confirm() in ngOnInit() function.
ERROR Error: Uncaught (in promise): Error: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference
JS: android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:224)
JS: android.app.AlertDialog$Builder.<init>(AlertDialog.java:454)
JS: com.tns.Runtime.callJSMethodNative(Native Method)
JS: com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1088)
JS: com.tns.Runtime.callJSMethodImpl(Runtime.java:970)
JS: com.tns.Runtime.callJSMethod(Runtime.java:957)
JS: com.tns.Runtime.callJSMethod(Runtime.java:941)
JS: com.tns.Runtime.callJSMethod(Runtime.java:933)
JS: com.tns.NativeScriptActivity.onStart(NativeScriptActivity.java:44)
JS: android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1334)
JS: android.app.Activity.performStart(Activity.java:7029)
JS: android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2741)
JS: android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
JS: android.app.ActivityThread.-wrap11(Unknown Source:0)
JS: android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
JS: android.os.Handler.dispatchMessage(Handler.java:106)
JS: android.os.Looper.loop(Looper.java:164)
JS: android.app.ActivityThread.main(ActivityThread.java:6494)
JS: java.lang.reflect.Method.invoke(Native Method)
JS: com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
JS: com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
JS: Error: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference
JS: android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:224)
JS: android.app.AlertDialog$Builder.<init>(AlertDialog.java:454)
JS: com.tns.Runtime.callJSMethodNative(Native Method)
JS: com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1088)
JS: com.tns.Runtime.callJSMethodImpl(Runtime.java:970)
JS: com.tns.Runtime.callJSMethod(Runtime.java:957)
JS: com.tns.Runtime.callJSMethod(Runtime.java:941)
JS: com.tns.Runtime.callJSMethod(Runtime.java:933)
JS: com.tns.NativeScriptActivity.onStart(NativeScriptActivity.java:44)
JS: android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1334)
JS: android.app.Activity.performStart(Activity.java:7029)
JS: android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2741)
JS: android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
JS: android.app.ActivityThread.-wrap11(Unknown Source:0)
JS: android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
JS: android.os.Handler.dispatchMessage(Handler.java:106)
JS: android.os.Looper.loop(Looper.java:164)
JS: android.app.ActivityThread.main(ActivityThread.java:6494)
JS: java.lang.reflect.Method.invoke(Native Method)
JS: com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
JS: com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
JS: at createAlertDialog (file:///data/data/org.nativescript.MarketingAssistant/files/app/tns_modules/tns-core-modules/ui/dialogs/dialogs.js:12:17)
JS: at file:///data/data/org.nativescript.MarketingAssistant/files/app/tns_modules/tns-core-modules/ui/dialogs/dialogs.js:121:27
JS: at new ZoneAwarePromise (file:///data/data/org.nativescript.MarketingAssistant/files/app/tns_modules/nativescript-angular/zone-js/dist/zone-nativescript.js:777:29)
JS: at Object.confirm (file:///data/data/org.nativescript.MarketingAssistant/files/app/tns_modules/tns-core-modules/ui/dialogs/dialogs.js:118:12)
...
But the same code woks well when being invoked by onTap() like function.
env:
nativescript 3.4.3
tns-core-modules 3.4.1
Hi @wanpeng2008,
In your scenario, you should way the UI to be loaded, before showing the alert. In this case, you can use ngAfterViewInit and set up timeout of 100 milliseconds, For example:
import { Component, OnInit } from "@angular/core";
import { Item } from "./item";
import { ItemService } from "./item.service";
import {Page} from "ui/page"
@Component({
selector: "ns-items",
moduleId: module.id,
templateUrl: "./items.component.html",
})
export class ItemsComponent implements OnInit {
items: Item[];
constructor(private itemService: ItemService, private page:Page) {
}
ngOnInit(): void {
this.items = this.itemService.getItems();
}
ngAfterViewInit(){
setTimeout(() => {
alert("Test alert");
}, 100);
}
}
Thanks @tsonevn . It works!
Most helpful comment
Hi @wanpeng2008,
In your scenario, you should way the UI to be loaded, before showing the alert. In this case, you can use ngAfterViewInit and set up timeout of 100 milliseconds, For example: