Android InstantiationException : No empty construc

2019-01-27 03:50发布

问题:

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"/>
...

回答1:

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.