在AndroidManifest.xml中声明的服务的上下文来自何处?

发布于 2025-02-01 03:51:19 字数 575 浏览 2 评论 0原文

我已经尝试访问服务的上下文(在androidManifest中声明),在其应用程序上犯了一个错误,例如:

class MyService : Service() {

    private val myObj = MyObjectThatNeedsContext(this)

    (...)
}

我只在Google中看到了问题。播放控制台,几个由nullpointerexception引起的崩溃,指向myobj的实例化。到目前为止还不错,但是我的问题来自于我在测试中从未发生过崩溃的事实,而只是发生在一部分用户,而不是全部。从理论上讲,当尝试实例化myService时,由于上下文在施工期间永远无法使用,它应该总是崩溃的,对吗?那么,为什么只偶尔会发生这种崩溃呢?

另外,在androidManifest中声明的服务中发生的崩溃是否会干扰应用程序的使用情况?当此服务用nullpoInterException崩溃时,我的应用程序的用户看到了什么?

谢谢。

I have made a mistake on my app by trying to access the context of a service (that was declared in the AndroidManifest) right at its constructor, like this:

class MyService : Service() {

    private val myObj = MyObjectThatNeedsContext(this)

    (...)
}

I only realized the problem after seeing in the Google Play Console a couple of crashes caused by NullPointerException, pointing at the instantiation of myObj. So far so good, but my question come from the fact that this crash never happened to me during my tests, and only happened to part of the users, not all. Theoretically it should always crash when trying to instantiate MyService, since the context is never available during construction, right? So why does this crash only happen occasionally?

Also, does a crash that happens in the creation of a service declared in the AndroidManifest interfere with the usage of the app? What did my app's user saw when this service crashed with a NullPointerException?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文