1 Replies - 108 Views - Last Post: 18 November 2018 - 04:15 AM

#1 Foobarer   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 269
  • Joined: 28-March 17

Problems with Google Location API

Posted 18 November 2018 - 04:10 AM

I am using a simple code from the Android Developer's site as shown here: Receive location updates, and keep getting

Quote

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);
            }
        };
    };
}


Is This A Good Question/Topic? 0
  • +

Replies To: Problems with Google Location API

#2 andrewsw   User is offline

  • Stealth IT
  • member icon

Reputation: 6735
  • View blog
  • Posts: 27,712
  • Joined: 12-December 12

Re: Problems with Google Location API

Posted 18 November 2018 - 04:15 AM

On what line is the error? Post the full error details.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1