xyz rety xyz rety - 4 months ago 55
Android Question

InstantiationException: has no zero argument constructor?

I'm trying to display a location address in google map but when i start Intent Service for the fetching address it will show error "Unable to instantiate service" .

i use this link for Displaying a Location Address.

https://developer.android.com/training/location/display-address.html


this is my code:

public class FetchAddressIntentService extends IntentService {
protected ResultReceiver mReceiver;

public FetchAddressIntentService(String name) {
super(name);
}


@Override
protected void onHandleIntent(Intent intent) {
String errorMessage="";

Geocoder geocoder=new Geocoder(this, Locale.getDefault());

Location location = intent.getParcelableExtra(Constants.LOCATION_DATA_EXTRA);



List<Address> addresses=null;

try {
addresses=geocoder.getFromLocation(location.getLatitude(),location.getLongitude(),1);
} catch (IOException e) {
errorMessage="unhendle IO exception";
e.printStackTrace();
}


if(addresses==null && addresses.size()==0){
if(errorMessage.isEmpty()){
errorMessage="no address found";
}

deliverResultToReceiver(Constants.FAILURE_RESULT, errorMessage);
}
else{

Address address=addresses.get(0);
ArrayList<String> arrayListFrag=new ArrayList<>();

for (int i=0;i<address.getMaxAddressLineIndex();i++){

arrayListFrag.add(address.getAddressLine(0));
}

deliverResultToReceiver(Constants.SUCCESS_RESULT,
TextUtils.join(System.getProperty("line.separator"),
arrayListFrag));

}
}

private void deliverResultToReceiver(int successResult, String message) {
Bundle bundle=new Bundle();
bundle.putString(Constants.RESULT_DATA_KEY,message);
mReceiver.send(successResult,bundle);
}
}


here i start intent Service:

protected void startIntentService(){
Intent intent=new Intent(this,FetchAddressIntentService.class);
intent.putExtra(Constants.RECEIVER, mResultReceiver);
intent.putExtra(Constants.LOCATION_DATA_EXTRA,mLastLocation);
startService(intent);
}


public void fetchAddressButtonHandler(){
if(mGoogleApiClient.isConnected() && mLastLocation!=null){
startIntentService();
}

}

Answer

Replace:

public FetchAddressIntentService(String name) {
  super(name);
}

with:

public FetchAddressIntentService() {
  super("whatever you want to use here");
}

(replacing the string with something suitable for your app)