0

This might be a silly question, but I don't get why a Service cannot access an Activity when it is instantiated/running. I have a static attribute (actually a HashMap) in my Activity, so I needed to access its content and copy to my Service (then be independent to run on the background). Whenever I try to access a class from my Service I get a NullPointer.

My code is basically this. Within the Activity:

// check if its static attribute is empty
startService(new Intent(MyService.class.getName()));

Within MyService (which extends Service):

serviceList = MainActivity.list; // Here I suppose I should clone my list 

I just didn't want to maybe send it through Parcel or Serialization, because I have custom objects in this HashMap.

Any solutions/explanation?

Teo Inke
  • 5,928
  • 4
  • 38
  • 37
  • Check this: http://stackoverflow.com/questions/2463175/how-to-have-android-service-communicate-with-activity – arlistan Apr 13 '15 at 15:09
  • Yes, I checked this topic before. But it's a different scenario and the answers are exactly what I am avoiding (since I am passing custom Objects, not just messages or events) – Teo Inke Apr 13 '15 at 15:40

1 Answers1

0

The answer was that I had my service running in a different process, so whenever I tried to access anything in my Activity, I got a NullPointerException.

Hope it helps someone in the future!

Teo Inke
  • 5,928
  • 4
  • 38
  • 37