Android InstantiationException : No empty constructor
Asked Answered
A

1

1

I get an InstantiationException when I try to start an IntentService. I have looked at other threads here but the answers don't solve my problem. My service class does have a default constructor. Here's my service class. (It is defined in a file FindMeService.java and that's the only class in that file.)

package com.findme.findme;
import android.app.IntentService;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;

public class FindMeService extends IntentService {

/*public static class Constants {       

}*/

public static final String BROADCAST_ACTION = "com.findme.findme.BROADCAST";
public static final String ACTION_REGISTER_USER = "com.findme.findme.REGISTER_USER";
public static final String KEY_USERID = "user_id";
public static final String KEY_USERPASS = "user_pass";
public static final String RESPONSE_FOR = "response_for";
public static final String RESPONSE_VAL = "response_val";

public FindMeService() {
    super("FindMeService");
}

@Override
protected void onHandleIntent(Intent intent) {
    // TODO handle intent
    if(intent.getAction() == ACTION_REGISTER_USER) {
        CommunicationsManager commManager = new CommunicationsManager();

        // get extras from the intent
        Bundle details = intent.getExtras();
        String userId = (String) details.get(KEY_USERID);
        String password = (String) details.get(KEY_USERPASS);

        // send the register request
        String result = commManager.Register(userId, password);

        // put the result into an intent and broadcast it
        Intent resultIntent = new Intent(BROADCAST_ACTION);
        resultIntent.putExtra(RESPONSE_FOR, ACTION_REGISTER_USER)
            .putExtra(RESPONSE_VAL, result);
        LocalBroadcastManager.getInstance(this).sendBroadcast(resultIntent);
    }
}

}

I am starting the service by making a call to startService() from inside an activity. The logs read as follows:

E/AndroidRuntime( 1048): java.lang.RuntimeException: Unable to instantiate service 
com.findme.findme.FindMeService: java.lang.InstantiationException: can't instantiate 
class com.findme.findme.FindMeService; no empty constructor

Here's how I am starting the service

...
Intent registerUserIntent = new Intent(this, FindMeService.class);
    registerUserIntent
            .setAction(FindMeService.ACTION_REGISTER_USER);
    registerUserIntent.putExtra(FindMeService.KEY_USERID,
            phoneNumber).putExtra(FindMeService.KEY_USERPASS,
            password);

    // start the service
    startService(registerUserIntent);
...

Here's a relevant part of the manifest file

....
<service
        android:name=".FindMeService"
        android:exported="false"
        android:enabled="true"/>
...
Abb answered 16/1, 2014 at 17:21 Comment(8)
@Raghunandan: No, that should be correct.Papilionaceous
Are you sure that the exception is coming from this version of the code? You might want to uninstall completely, then try it again, just to be sure.Papilionaceous
@Papilionaceous you are right verified developer.android.com/reference/android/app/…Farrish
Do you mean uninstall the app from the emulator? Yeah, I tried that. No luck.Abb
Well, what you have looks fine from where I sit.Papilionaceous
Added some more relevant code. Hope it helps.Abb
is that package correct? "package com.findme.findme" Is this the same package specified in the AndroidManifest?Gombosi
@AndrewG Yes, the package is correct.Abb
A
2

The problem was finally solved and it turns out that it was due to some bug in either the emulator or in Eclipse. The problem was solved after I restarted Eclipse and unchecked the "Launch form snapshot" option and checked the "Wipe user data" option before launching the emulator. Thanks every one for looking into this question. I too was totally baffled about the entire matter.

Abb answered 17/1, 2014 at 12:59 Comment(1)
THis is not final solutionPatriarchy

© 2022 - 2024 — McMap. All rights reserved.