Android RuntimeException: Unable to instantiate the service
In your concrete implementation you have to declare a default constructor which calls the public IntentService (String name)
super constructor of the abstract IntentService class you extend:
public MyService () {
super("MyServerOrWhatever");
}
You do not need to overwrite onStartCommand if the super implementation fits for you (what I expect).
In your current case you should get an exception (Unable to instantiate service...) - it is always worth to put this in the question.
java.lang.RuntimeException unable to instantiate service: java.lang.NullPointerException
You cannot call getSystemService()
from an initializer (as you originally were) or from the constructor (as you are now). Please override onCreate()
, and call getSystemService()
there.
Basically, until onCreate()
is called (or, more accurately, until after you call super.onCreate()
from your onCreate()
), the inherited methods on Service
are not yet ready for your use.
java.lang.RuntimeException: Unable to instantiate service .GCMIntentService
You should have GCMIntentService
in the client code and you should define it in the AndroidManifest.xml file as follows.
<service android:name="package_name.GCMIntentService" />
Note that name of the intent service should be exactly same as GCMIntentService
if you use GCMBroadcastReceiver
. You can create your intent service if you implement your broadcast receiver.
java.lang.RuntimeException: Unable to instantiate service com.google.android.gcm.GCMBroadCastReceiver
In Manifest you wrote:
android:name="com.google.android.gcm.GCMBroadcastReceiver"
But your GcmBroadcastReceiver.java
is in package net.myapp
Change android:name="com.google.android.gcm.GCMBroadcastReceiver"
to android:name="net.myapp.GcmBroadcastReceiver"
java.lang.RuntimeException: Unable to instantiate service
public class SMSMonitor extends BroadcastReceiver {
you are SMSMonitor which is a BroadcastReceiver not Service
Intent i = new Intent(context,SMSMonitor.class);
i.setClass(context, SMSMonitor.class);
context.startService(i);
Why do I get an InstantiationException if I try to start a service?
You service needs to have a public no-args constructor. Otherwize, Android will not be able to instantiate it.
So replace
public StatisticsWidgetUpdateService(String name) {
super(name);
}
with
public StatisticsWidgetUpdateService() {
super("SOME NAME");
}
Related Topics
Convert Arraylist<Mycustomclass> to JSONarray
How to Simulate Touch from Background Service with Sendevent or Other Way
Using Resultreceiver in Android
Redraw a Single Row in a Listview
Android SQLite How to Check If a Record Exists
Source of Android's Lock Screen
How to Determine If One of My Activities Is in the Foreground
Toolbar Navigation Icon Never Set
Android Studio Not Deploying Changes to App
Using Seekbar to Control Volume in Android
Android: Scrollview Force to Bottom
Opening Android Settings Programmatically
How to Customize Item Background and Item Text Color Inside Navigationview
Actionbarsherlock + Tabs + Multi Fragments