- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 IntentService
Android 提供的功能尝试在后台运行一些计算。
我从昨天开始就一直在研究这个问题,但似乎找不到任何有效的解决方案。
我已经查看了很多有关 IntentService 的问题,并尝试应用建议的修复程序,但我的似乎无法工作。
我尝试过传递各种不同的 context
/ Activity/getApplicationContext()
/this
/getBaseContext()
从我调用该函数的 Activity 中,但我仍然收到 空对象引用 错误
"Attempt to invoke virtual method 'android.content.ComponentName android.content.Context.startService(android.content.Intent)' on a null object reference"
应用程序在函数的第 3 行崩溃,其中 startService()
被调用(请参阅下面的代码)
//This function is from MyCurrentClass.class - it is a Singleton and its being called from another activity where it passes in its context
public void notify (Context context, JSONObject responseFromDb){
Intent inputIntent = new Intent(context, MyCurrentClass.class);
inputIntent.putExtra("responseFromDB", responseFromDb.toString());
this.startService(inputIntent); //crashes here!
}
P.S.:我看过的大多数解决方案似乎只有 1 个 Activity,但我的应用程序正在不同的 Activity 之间切换。(不确定这是否重要)但最重要的是我正在处理的类是一个扩展 IntentService 的普通 Java 类,而不是 Activity 类。
我还在我的 Android list 中声明了该服务以链接到 MyCurrentClass 并尝试 bindService()
从 Activity 中:它被调用了,但它也不起作用。
以下是我查看并尝试过但失败的一些问题/解决方案:
我实际上看过很多其他帖子,但太多了。
希望我提供的信息足够。如果不是,请告诉我可能缺少什么,我将使用所需的信息更新问题。
最佳答案
使用context
,就像您在从 Activity 中收到的评论中提到的那样,因为在这种情况下,您当前的类没有上下文。 read for further details
context.startService(inputIntent);
而不是
this.startService(inputIntent);
关于java - IntentService - startService 上的 NullPointerExecption,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39483658/
Context.startService(intent) 和 startService(intent) 有什么区别,使用哪个有关系吗? 最佳答案 只有一个startService() 方法。 star
这只是一个知识问题,我知道这样做不是一个好的做法。所以这里是: 如果我调用 startService() 3 次而它已经在运行,我是否必须调用 stopService() 3 次才能让它停止? (它有
我有一个依赖 X 的 Windows 服务 S1。该服务是自动的,它可以毫无问题地启动。 此服务启动另一个具有相同依赖项 X 的服务 S2。问题是,当我调用 StartService() 启动 S2
我尝试从服务类启动 android 服务。这样做的原因是为了实现一些平台独立性。 这样做我在 android.content.ContextWrapper.startService(ContextWr
我有这个函数作为静态方法: public static void engine(SQLiteDatabase mydb){ Intent intent = new Intent
我在文档中的任何地方都找不到这个: 对 context.startService() 的调用是同步的还是异步的? 最佳答案 异步。它甚至不会开始做任何工作,直到您从您所在的任何回调(例如,onCrea
startService() 是创建一个新的服务实例还是使用现有的?例如,在下面的代码中,它是创建两个 UpdateService 实例还是只创建一个 UpdateService 实例?谢谢。
我正在尝试安装 Neo4j(1.8),但在启动服务时遇到问题。 解压zip文件后,我直接进入bin文件夹并执行批处理文件: Neo4J.bat install [SC] 创建服务成功 [SC] 启动
我正在 Delphi 10.2 pro 中编写一些服务应用程序,我想添加一个启动时间可控参数来强制服务应用程序进入启动等待循环足够长的时间,以便我可以单击进入“运行\附加到进程”窗口(在应用程序开始初
我有一个小脚本,用户可以在其中输入脚本名称(描述名称),并可以选择修改属性。这是通过GUI界面完成的:一个文本框和一些下拉列表。 我尝试了几种方法来实现它,但是没有一个起作用。 这是我如何检索服务信息
这件事让我非常头疼。 我正在尝试使用 CreateService 和 SERVICE_ALL_ACCESS 将驱动程序加载到进程中,在我实际尝试使用 StartService 启动服务之前,一切似乎都
这就是我启动位置服务的方式。 private void initLocationService() { locationManager = (LocationManager) getSyste
我已经创建了一个服务,我正在使用 bindService 函数通过我的 Activity 访问它,但我想在我的 Activity 退出后继续运行我的服务。 为此,我必须使用 startService,
我正在尝试在Windows 10(10.0.18362)上安装自签名内核驱动程序,这里是我遵循的步骤以及出现的错误。如何解决这个问题?谢谢 c:\>makecert -r -pe -ss Privat
我想从我的 Activity 类(class)中启动一项服务。问题是,onStartCommand 永远不会被调用。我在那里放置了断点,但它从未被击中。我的例子非常简单,我不明白为什么它不起作用。 M
我通过 Intent 在我的 Activity 和我的服务之间发送数据(我多次调用 startForegroundService 因为它再次调用 onStartCommand 并使用 getInten
这个问题已经有答案了: What is a NullPointerException, and how do I fix it? (12 个回答) 已关闭 6 年前。 我正在尝试使用 IntentSe
在 while(true) 语句中,我正在创建一个将在 onHandleIntent 中运行案例的 Intent 。但是,如果不将 Activity 类扩展到我的 LocationTracker 类,
我想用 C++ 程序启动卷影复制服务。这涉及获取服务控制管理器 (OpenSCManager) 的句柄,使用它来获取服务句柄 (OpenService),然后启动服务 (StartService)。
我继承了一个项目,但我不明白如何通过以下代码获得响应或设置回调: startService(new Intent(this, VenueUpdateService.class)); 目前,即使 Ven
我是一名优秀的程序员,十分优秀!