I'm trying to start an IntentService
from a clickhandler in my main activity. I'm studying Intents
right now but don't quite get it yet. I'm not sure how I'm supposed to instantiate my intent here and pass it to startService
. I don't know why I have to do such a thing, the Intent
will not be used within my service, that I know of. I don't know how to debug this problem.
From the click callback:
this.commute = new Commute();
locationService = new LocationService();
locationService.setCommute(commute);
// com.orm.SugarApp@8b2e18f is this.getApplicationContext() ...
Context context = this.getBaseContext();
System.out.println("!!!!!!!!!!!!!!!!!!!!!");
System.out.println(context);
Intent intent = new Intent(context, LocationService.class);
System.out.println(locationService);
System.out.println(intent);
locationService.startService(intent); // <---- OFFENDING LINE
System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@");
chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.start();
commute.start();
logcat before traceback, none of the objects are null
...:
01-27 09:53:44.465 2718-2718/org.skyl.commutetracker I/System.out﹕ !!!!!!!!!!!!!!!!!!!!!
01-27 09:53:44.466 2718-2718/org.skyl.commutetracker I/System.out﹕ android.app.ContextImpl@8b2e18f
01-27 09:53:44.466 2718-2718/org.skyl.commutetracker I/System.out﹕ org.skyl.commutetracker.services.LocationService@387dcc1c
01-27 09:53:44.466 2718-2718/org.skyl.commutetracker I/System.out﹕ Intent { cmp=org.skyl.commutetracker/.services.LocationService }
01-27 09:53:44.466 2718-2718/org.skyl.commutetracker D/AndroidRuntime﹕ Shutting down VM
01-27 09:53:44.481 2718-2718/org.skyl.commutetracker E/AndroidRuntime﹕ FATAL EXCEPTION: main
This causes the following traceback:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.ComponentName android.content.Context.startService(android.content.Intent)' on a null object reference
at android.content.ContextWrapper.startService(ContextWrapper.java:515)
at org.skyl.commutetracker.MainActivity.startCommute(MainActivity.java:86)
at org.skyl.commutetracker.MainActivity.toggleClick(MainActivity.java:67)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)