I'm trying to use the IntentService
feature that Android provides to try running some calculations in the background.
I've been working on this since yesterday, but I can't seem to find any valid solution.
I've looked at quite a few SO questions with regards to IntentService and tried applying the suggested fixes, but mine just can't seem to work.
I've tried passing in various different context
/ Activies / getApplicationContext()
/ this
/ getBaseContext()
from the Activity where I call the function but I'm still getting a null object reference error
"Attempt to invoke virtual method 'android.content.ComponentName android.content.Context.startService(android.content.Intent)' on a null object reference"
Where the application crashes at the 3rd line in the function where startService()
is called (see my code below)
//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.: Most of the solutions I've looked at seem to have only 1 Activity, but my app is switching between different Activities. (Not sure if that matters) But most importantly the class I'm working on is a normal Java class that extends IntentService and its not an Activity class.
I've also declared the service in my Android manifest to link to MyCurrentClass and tried bindService()
from the Activity: it was called, but it didn't work too.
Below are some of the questions/solutions I've looked at and tried but failed:
- 'java.lang.String android.content.Context.getPackageName()' on a null object reference
- Trying to start a service on android and get a null object
- Starting Android IntentService with explicit intent NullPointerException
- How to get Context in an Intent Service
I've actually looked at many others posts, but there were too many.
Hope the info I've provided is enough. If it isn't, do let me know what might be missing and I'll update the question with the required info.