0

When I am trying to launch a call activity from a Service, I get a NullPointerException. Here is my code:

Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + number));
callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(callIntent);

I get the exception on the startActivity line.

I tried to use getApplication.startActivity and getApplicationContext.startActivity but no luck.

Any ideas?

edit : Maybe some usefull info: I am trying to create a service that will run on the background and scan sensor data, when a certain signal is given i would like to maken an automated call to a number.

edit : full adb error code:

03-31 09:04:10.214: ERROR/AndroidRuntime(1896): Uncaught handler: thread main exiting due to uncaught exception
03-31 09:04:10.226: ERROR/AndroidRuntime(1896): java.lang.RuntimeException: Unable to instantiate service dfz.epilepsiedetector.services.DetectionService: java.lang.NullPointerException
03-31 09:04:10.226: ERROR/AndroidRuntime(1896):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:2668)
03-31 09:04:10.226: ERROR/AndroidRuntime(1896):     at android.app.ActivityThread.access$3100(ActivityThread.java:116)
03-31 09:04:10.226: ERROR/AndroidRuntime(1896):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1846)
03-31 09:04:10.226: ERROR/AndroidRuntime(1896):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-31 09:04:10.226: ERROR/AndroidRuntime(1896):     at android.os.Looper.loop(Looper.java:123)
03-31 09:04:10.226: ERROR/AndroidRuntime(1896):     at android.app.ActivityThread.main(ActivityThread.java:4203)
03-31 09:04:10.226: ERROR/AndroidRuntime(1896):     at java.lang.reflect.Method.invokeNative(Native Method)
03-31 09:04:10.226: ERROR/AndroidRuntime(1896):     at java.lang.reflect.Method.invoke(Method.java:521)
03-31 09:04:10.226: ERROR/AndroidRuntime(1896):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
03-31 09:04:10.226: ERROR/AndroidRuntime(1896):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
03-31 09:04:10.226: ERROR/AndroidRuntime(1896):     at dalvik.system.NativeStart.main(Native Method)
03-31 09:04:10.226: ERROR/AndroidRuntime(1896): Caused by: java.lang.NullPointerException
03-31 09:04:10.226: ERROR/AndroidRuntime(1896):     at android.content.ContextWrapper.getPackageName(ContextWrapper.java:120)
03-31 09:04:10.226: ERROR/AndroidRuntime(1896):     at android.content.ComponentName.<init>(ComponentName.java:75)
03-31 09:04:10.226: ERROR/AndroidRuntime(1896):     at android.content.Intent.<init>(Intent.java:2302)
03-31 09:04:10.226: ERROR/AndroidRuntime(1896):     at dfz.epilepsiedetector.services.DetectionService.<init>(DetectionService.java:35)
03-31 09:04:10.226: ERROR/AndroidRuntime(1896):     at java.lang.Class.newInstanceImpl(Native Method)
03-31 09:04:10.226: ERROR/AndroidRuntime(1896):     at java.lang.Class.newInstance(Class.java:1472)
03-31 09:04:10.226: ERROR/AndroidRuntime(1896):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:2665)

edit Trimmed class code:

public class DetectionService extends IntentService implements SensorEventListener
{   
 private SensorManager mSensorManager;
 private Sensor mAccelerometer;
 private boolean hasSeizure = false;

 private final int POLLS_PER_SECOND = 10;

 public DetectionService()
 {
 super("EpilepsionDetectionService");

 Intent callIntent = new Intent(DetectionService.this,
 InformationActivity.class);
 callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);        
 getApplication().startActivity(callIntent);
}
Siddharth
  • 9,349
  • 16
  • 86
  • 148
nldev
  • 299
  • 3
  • 15
  • can you try ((Activity)xyz.getContext()).startActivity(it); – LTEHUB Mar 31 '11 at 08:14
  • Sorry what do you mean by xyz? – nldev Mar 31 '11 at 08:28
  • xyz is your service; http://stackoverflow.com/questions/3456034/how-to-start-an-activity-from-a-service ; some guy did the same with you. do you have a permission in ANdroidManifest ? – LTEHUB Mar 31 '11 at 08:55
  • @DO Ngoc Tuan i am calling the intent in that way, the problem is that its not working! i keep recieveing `NullPointerException` – nldev Mar 31 '11 at 08:59
  • i did the same with you before: get the value of Sensor, if(condition) call new Activity. no problem. ; - - -- - - - - public class MyclassService extends MapActivity implements SensorEventListener{ – LTEHUB Mar 31 '11 at 09:45
  • Yes i know, i am also able to react to sensors and launch activity's but just not from a background service, what you mention is a MapActivity. Ofcourse they both have a context but when I try to get it from the service it returns null. – nldev Mar 31 '11 at 09:57
  • MapActivity is in my situation;or you can see here , call the activity from Service, it doesnot solve your problem but it give you some idea : - - - - -http://stackoverflow.com/questions/2735927/running-activity-from-remote-service – LTEHUB Mar 31 '11 at 11:37

5 Answers5

1

You have to get that context from an activity where you calling a service.. from that you have to call`

Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + number));


 callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 context.startActivity(callIntent);

edit:

callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(callIntent);`

`

hope it helps...

Uday
  • 5,933
  • 9
  • 44
  • 76
0

You can try:

getBaseContext().startActivity(callIntent);

Or use:

this.startActivity(callIntent);
vendor
  • 1,854
  • 13
  • 8
0

For future reference:

The problem went away when inhereting directly from Service as opposed to Intent service.

Hop this helps!

nldev
  • 299
  • 3
  • 15
0

Move the code out of the constructor and into the onCreate() method. The long and short of it is that the class hasn't been totally initialized yet and is causing the NullPointerException.

wattostudios
  • 8,666
  • 13
  • 43
  • 57
Dallas
  • 1
-1

Call the intent in the onStart method of the service instead of the onCreate method.

user
  • 86,916
  • 18
  • 197
  • 190
Anirudh
  • 2,767
  • 5
  • 69
  • 119