pileup
pileup

Reputation: 3292

Problems with Google Location API

I am using a simple code and keep getting

java.lang.NullPointerException: Listener must not be null

I am only using a very short and simple code to try and get my current location, yet I can't get it to work. It might has to do something with that "null" at the looper place in the requestLocationUpdates callback. But I am not sure. I've been trying all day already.

Here is the short code:

public class MainActivity extends AppCompatActivity {
private FusedLocationProviderClient mFusedLocationClient;
private LocationCallback mLocationCallback;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);

    LocationRequest mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(10000);
    mLocationRequest.setFastestInterval(5000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {
        //
    } else {
        mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback,
                null /* Looper */);
    }

    mLocationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(LocationResult locationResult) {
            if (locationResult == null) {
                return;
            }
            for (Location location : locationResult.getLocations()) {
                // Update UI with location data
                TextView tv = (TextView) findViewById(R.id.tv);
                Double latDouble = location.getLatitude();
                String latString = latDouble.toString();
                tv.setText(latString);
            }
        };
    };
}

All I want is to simply get current location, why is it giving me such hard times? What causes this error?

thanks

Upvotes: 0

Views: 339

Answers (1)

Nikos Hidalgo
Nikos Hidalgo

Reputation: 3766

You're trying to use mLocationCallback before you initialise it. Just move this code :

mLocationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(LocationResult locationResult) {
            if (locationResult == null) {
                return;
            }
            for (Location location : locationResult.getLocations()) {
                // Update UI with location data
                TextView tv = (TextView) findViewById(R.id.tv);
                Double latDouble = location.getLatitude();
                String latString = latDouble.toString();
                tv.setText(latString);
            }
        };
    };

before this:

if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {
        //
    } else {
        mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback,
                null /* Looper */);
    }

The null in the Looper thread has nothing to do with it. By passing it as null you keep your callbacks on the calling thread.

Upvotes: 2

Related Questions