如何处理uncaughtErrorEvent&;在NativeScript中写入日志时发生异常?

如何处理uncaughtErrorEvent&;在NativeScript中写入日志时发生异常?,nativescript,Nativescript,我在{N}@2.0.1中尝试了上面的例子,但它对我不起作用 您能解释更多细节吗?在NativeScript github存储库中回答如下: application.uncaughtErrorEvent将在应用程序崩溃且原因未得到处理时触发 例如: 如果您尝试在main-page.js中使用空上下文初始化android按钮,它将抛出一个错误 在main-page.js中 var application = require("application"); application.on(applic

我在{N}@2.0.1中尝试了上面的例子,但它对我不起作用


您能解释更多细节吗?

在NativeScript github存储库中回答如下:

application.uncaughtErrorEvent将在应用程序崩溃且原因未得到处理时触发

例如: 如果您尝试在main-page.js中使用空上下文初始化android按钮,它将抛出一个错误

在main-page.js中

var application = require("application");

application.on(application.uncaughtErrorEvent, function (args) {
    if (args.android) {
        // For Android applications, args.android is an NativeScriptError.
        console.log("NativeScriptError: " + args.android);
    } else if (args.ios) {
        // For iOS applications, args.ios is NativeScriptError.
        console.log("NativeScriptError: " + args.ios);
    }
});
从日志中可以看到以下信息:

var btn = new android.widget.Button(null);
What you can do to catch this error is to use uncaughtErrorEvent

in app.js

"use strict";
var application = require("application");

application.on(application.uncaughtErrorEvent, function (args) {
    if (args.android) {
        // For Android applications, args.android is an NativeScriptError.
        console.log(" *** NativeScriptError *** : " + args.android);
        console.log(" *** StackTrace *** : " + args.android.stackTrace);
        console.log(" *** nativeException *** : " + args.android.nativeException);
    }
    else if (args.ios) {
        // For iOS applications, args.ios is NativeScriptError.
        console.log("NativeScriptError: " + args.ios);
    }
});
application.start({ moduleName: "main-page" });
从stackTrace可以看到此错误的原因:

NativeScriptError : Error: The application crashed because of an uncaught exception.
您还有一个发生错误的指针:

 Attempt to invoke virtual method 'android.content.res.Resources 
 android.content.Context.getResources()' on null reference object

第11行第15列在NativeScript github存储库中回答如下:

application.uncaughtErrorEvent将在应用程序崩溃且原因未得到处理时触发

例如: 如果您尝试在main-page.js中使用空上下文初始化android按钮,它将抛出一个错误

在main-page.js中

var application = require("application");

application.on(application.uncaughtErrorEvent, function (args) {
    if (args.android) {
        // For Android applications, args.android is an NativeScriptError.
        console.log("NativeScriptError: " + args.android);
    } else if (args.ios) {
        // For iOS applications, args.ios is NativeScriptError.
        console.log("NativeScriptError: " + args.ios);
    }
});
从日志中可以看到以下信息:

var btn = new android.widget.Button(null);
What you can do to catch this error is to use uncaughtErrorEvent

in app.js

"use strict";
var application = require("application");

application.on(application.uncaughtErrorEvent, function (args) {
    if (args.android) {
        // For Android applications, args.android is an NativeScriptError.
        console.log(" *** NativeScriptError *** : " + args.android);
        console.log(" *** StackTrace *** : " + args.android.stackTrace);
        console.log(" *** nativeException *** : " + args.android.nativeException);
    }
    else if (args.ios) {
        // For iOS applications, args.ios is NativeScriptError.
        console.log("NativeScriptError: " + args.ios);
    }
});
application.start({ moduleName: "main-page" });
从stackTrace可以看到此错误的原因:

NativeScriptError : Error: The application crashed because of an uncaught exception.
您还有一个发生错误的指针:

 Attempt to invoke virtual method 'android.content.res.Resources 
 android.content.Context.getResources()' on null reference object

行:11,列:15`

你好,尼克。从我的测试-这将仍然显示崩溃屏幕。有没有办法不显示错误屏幕而忽略它?嗨,尼克。从我的测试-这将仍然显示崩溃屏幕。有没有办法不显示错误屏幕并忽略它?