Amit Laloush
Lurker
Hey
I'm trying to get a location with "FusedLocationProviderApi".
I want to create a "Location" class that will connect to the googleApiClient.
I am building an object in the mainActivity and when the constructor invoked I will want to initialize two instance variable "longitude and latitude".
but I'm getting an exception in the constructor
"java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Looper android.content.Context.getMainLooper()' on a null object reference"
thanks for the help
I'm trying to get a location with "FusedLocationProviderApi".
I want to create a "Location" class that will connect to the googleApiClient.
I am building an object in the mainActivity and when the constructor invoked I will want to initialize two instance variable "longitude and latitude".
but I'm getting an exception in the constructor
"java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Looper android.content.Context.getMainLooper()' on a null object reference"
thanks for the help

Java:
public class findLocation extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener, LocationListener {
private FusedLocationProviderApi locationProvider = LocationServices.FusedLocationApi;
private final static int MILLISECONDS = 1000;
private final static int TIME_FOR_INTERVAL = 60* MILLISECONDS;
private boolean isConnected;
android.location.Location mLastLocation;
private GoogleApiClient googleApiClient;
private LocationRequest locationRequest;
double lat,lon;
public findLocation() {
googleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).addApi(AppIndex.API).build();
locationRequest = new LocationRequest();
locationRequest.setInterval(TIME_FOR_INTERVAL);
locationRequest.setFastestInterval(1500);
googleApiClient.connect();
isConnected = googleApiClient.isConnected();
}
public boolean isConnected() {
return isConnected;
}
@Override
public void onConnected(Bundle bundle) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onLocationChanged(Location location) {
lon = location.getLongitude();
lat = location.getLatitude();
LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this);
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
Last edited: