I'm not sure whats wrong in it! I read here that Intentservice is itself a subclass of Context
.
public class GCMNotificationIntentService extends IntentService {
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
Context mainContext;
WriteFile writeFile;
public GCMNotificationIntentService() {
super("GcmIntentService");
mainContext = getApplicationContext();
writeFile = new WriteFile(mainContext);
}
// My rest of the code
}
but I'm getting null value for mainContext
.
Any suggestions are most welcome.
In the constructor it is way too early to access the app context. Try to move this code into the onCreate
method.
More datails about the life cycle can be found in the documentation
You should be calling this in your onCreate method, not the constructor. In the constructor, the application context has not been set up yet, so it will be null.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With