David Caunt

Mobile Software Engineer

Subscribe to my RSS feed

Android Gotcha – Unable to instantiate service

October 21st, 2011

If you’re using Eclipse and create an IntentService class using the IDE’s correction features you’ll create a class like this:

public class MyService extends IntentService {
 
    public MyService(String name) {
        super(name);
        // TODO Auto-generated constructor stub
    }
 
    @Override
    protected void onHandleIntent(Intent intent) {
        // TODO Auto-generated method stub        
        Log.d("MyService", "onHandleIntent (intent=" + intent + ")");
    }
}

I’ve added a Log call so I can see my service receive intents.

When you trigger the service in your application, you’ll immediately see this cryptic force close error in your logcat:

E/AndroidRuntime( 7758): FATAL EXCEPTION: main
E/AndroidRuntime( 7758): java.lang.RuntimeException: Unable to instantiate service com.example.brokenservice.service.MyService: java.lang.InstantiationException: com.example.brokenservice.service.MyService
E/AndroidRuntime( 7758): 	at android.app.ActivityThread.handleCreateService(ActivityThread.java:2177)
E/AndroidRuntime( 7758): 	at android.app.ActivityThread.access$2500(ActivityThread.java:132)
E/AndroidRuntime( 7758): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1102)
E/AndroidRuntime( 7758): 	at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 7758): 	at android.os.Looper.loop(Looper.java:143)
E/AndroidRuntime( 7758): 	at android.app.ActivityThread.main(ActivityThread.java:4263)
E/AndroidRuntime( 7758): 	at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 7758): 	at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 7758): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime( 7758): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime( 7758): 	at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 7758): Caused by: java.lang.InstantiationException: com.example.brokenservice.service.MyService
E/AndroidRuntime( 7758): 	at java.lang.Class.newInstanceImpl(Native Method)
E/AndroidRuntime( 7758): 	at java.lang.Class.newInstance(Class.java:1409)
E/AndroidRuntime( 7758): 	at android.app.ActivityThread.handleCreateService(ActivityThread.java:2174)
E/AndroidRuntime( 7758): 	... 10 more

What gives?

The clue is in the java.lang.InstantiationException, which should be the first thing you search for. This suggests an issue with the constructor for MyService.

The problem is that the OS is looking for a constructor with no arguments, while the definition of IntentService has a constructor which takes a String name. This constructor is overridden by Eclipse. The correct definition of MyService looks like this:

public class MyService extends IntentService {
 
    public MyService() {
        super("MyService");
        // TODO Auto-generated constructor stub
    }
 
    @Override
    protected void onHandleIntent(Intent intent) {
        // TODO Auto-generated method stub      
        Log.d("MyService", "onHandleIntent (intent=" + intent + ")");
    }
}

Thanks to ddewaele on Stack Overflow for the correct answer.

Tags:

Leave a Reply

You may wrap code in <code> tags or use <pre lang="php">code</pre> for full highlighting.

Top