在AndroidManifest.xml中声明的服务的上下文来自何处?
我已经尝试访问服务的上下文(在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论