-1
E/FetchAddressIS: No receiver received. There is nowhere to send the results.

I'm trying to use an intent Service to get a street name from a lastKnownLocation object. It's throwing a hissy, and I see no reason why.

The code is from Google developers, it was exactly what I needed, except the not working bit.

I've tried (1) changing the package name in Constants, well I needed to do that sure! But still the error. I've trawled through my MainActivity to see if I somehow mutate LastKnownLocation, or don't assign it before calling, but it always has a valid value.

So I'm now at a loss, and would love some expert eyes and learning. Plug and learn instead of plug and pray.

    package com.example.android.recyclingbanks;

import android.app.IntentService;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.os.Bundle;
import android.os.ResultReceiver;
import android.text.TextUtils;
import android.util.Log;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * Asynchronously handles an intent using a worker thread. Receives a ResultReceiver object and a
 * location through an intent. Tries to fetch the address for the location using a Geocoder, and
 * sends the result to the ResultReceiver.
 */
public class FetchAddressIntentService extends IntentService {
    private static final String TAG = "FetchAddressIS";

    /**
     * The receiver where results are forwarded from this service.
     */
    protected ResultReceiver mReceiver;

    /**
     * This constructor is required, and calls the super IntentService(String)
     * constructor with the name for a worker thread.
     */
    public FetchAddressIntentService() {
        // Use the TAG to name the worker thread.
        super(TAG);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.wtf("onHandleIntent", "error already?!");
        String errorMessage = "";

        mReceiver = intent.getParcelableExtra(Constants.RECEIVER);

        // Check if receiver was properly registered.
        if (mReceiver == null) {
            Log.wtf(TAG, "No receiver received. There is nowhere to send the results.");
            return;
        }

        // Get the location passed to this service through an extra.
        Location location = intent.getParcelableExtra(Constants.LOCATION_DATA_EXTRA);

        // Make sure that the location data was really sent over through an extra. If it wasn't,
        // send an error error message and return.
        if (location == null) {
            errorMessage = getString(R.string.no_location_data_provided);
            Log.wtf(TAG, errorMessage);
            deliverResultToReceiver(Constants.FAILURE_RESULT, errorMessage);
            return;
        }

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

        // Address found using the Geocoder.
        List<Address> addresses = null;

        try {
            addresses = geocoder.getFromLocation(
                    location.getLatitude(),
                    location.getLongitude(),
                    // In this sample, we get just a single address.
                    1);
        } catch (IOException ioException) {
            // Catch network or other I/O problems.
            errorMessage = getString(R.string.service_not_available);
            Log.e(TAG, errorMessage, ioException);
        } catch (IllegalArgumentException illegalArgumentException) {
            // Catch invalid latitude or longitude values.
            errorMessage = getString(R.string.invalid_lat_long_used);
            Log.e(TAG, errorMessage + ". " +
                    "Latitude = " + location.getLatitude() +
                    ", Longitude = " + location.getLongitude(), illegalArgumentException);
        }

        // Handle case where no address was found.
        if (addresses == null || addresses.size()  == 0) {
            if (errorMessage.isEmpty()) {
                errorMessage = getString(R.string.no_address_found);
                Log.e(TAG, errorMessage);
            }
            deliverResultToReceiver(Constants.FAILURE_RESULT, errorMessage);
        } else {
            Address address = addresses.get(0);
            ArrayList<String> addressFragments = new ArrayList<String>();


            for(int i = 0; i < address.getMaxAddressLineIndex(); i++) {
                addressFragments.add(address.getAddressLine(i));
            }
            Log.i(TAG, getString(R.string.address_found));
            deliverResultToReceiver(Constants.SUCCESS_RESULT,
                    TextUtils.join(System.getProperty("line.separator"), addressFragments));
        }
    }

    /**
     * Sends a resultCode and message to the receiver.
     */
    private void deliverResultToReceiver(int resultCode, String message) {
        Bundle bundle = new Bundle();
        bundle.putString(Constants.RESULT_DATA_KEY, message);
        mReceiver.send(resultCode, bundle);
    }
}

Help me please! This is my apps last deal breaker issue, geo-coding from gps is a nice little touch for the user.

Sam
  • 1,659
  • 4
  • 23
  • 42
  • 1
    How is the Intent created in the MainActivity to start, and call `onHandleIntent` in the `IntentService`? – Mark Sep 20 '16 at 23:21
  • 1
    Do you have the parcelable CREATOR in your ResultReciever? Are you creating it as an anonymous or non-static inner class? – FunkTheMonk Sep 21 '16 at 00:50
  • 1
    the `Intent`you are sending to `FetchAddressIntentService` has no `Constants.RECEIVER` extra which should be a `ResultReciever` object instance – pskink Sep 21 '16 at 06:02

1 Answers1

0

Thanks for your attention guys, you have great insight, I looked back at it and the problem was with MainActivity, I hadn't intialised

mResultReceiver = new AddressResultReceiver(new Handler());

Looks like I needed to search a bit longer, so duplicate!

ResultReceiver gives Nullpointer exception

Community
  • 1
  • 1
Sam
  • 1,659
  • 4
  • 23
  • 42