1. Download our Official Android App: Forums for Android!

Apps Find location with FusedLocationProviderApi

Discussion in 'Android Development' started by Amit Laloush, Apr 14, 2016.

  1. Amit Laloush

    Amit Laloush Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    2
    Joined:
    Apr 14, 2016

    Apr 14, 2016
    2
    0
    5
    Male
    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 :)

    Code (Java):
    1. public class findLocation extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,
    2.         GoogleApiClient.OnConnectionFailedListener, LocationListener {
    3.  
    4.     private FusedLocationProviderApi locationProvider = LocationServices.FusedLocationApi;
    5.     private final static int MILLISECONDS = 1000;
    6.     private final static int TIME_FOR_INTERVAL = 60* MILLISECONDS;
    7.     private boolean isConnected;
    8.  
    9.     android.location.Location mLastLocation;
    10.     private GoogleApiClient googleApiClient;
    11.     private LocationRequest locationRequest;
    12.     double lat,lon;
    13.  
    14.     public findLocation() {
    15.  
    16.             googleApiClient = new GoogleApiClient.Builder(this)
    17.                     .addApi(LocationServices.API)
    18.                     .addConnectionCallbacks(this)
    19.                     .addOnConnectionFailedListener(this).addApi(AppIndex.API).build();
    20.             locationRequest = new LocationRequest();
    21.             locationRequest.setInterval(TIME_FOR_INTERVAL);
    22.             locationRequest.setFastestInterval(1500);
    23.             googleApiClient.connect();
    24.             isConnected = googleApiClient.isConnected();
    25.         }
    26.  
    27.     public boolean isConnected() {
    28.         return isConnected;
    29.     }
    30.  
    31.     @Override
    32.     public void onConnected(Bundle bundle) {
    33.         if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    34.             return;
    35.         }
    36.         LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
    37.     }
    38.  
    39.     @Override
    40.     public void onConnectionSuspended(int i) {
    41.  
    42.     }
    43.  
    44.     @Override
    45.     public void onLocationChanged(Location location) {
    46.         lon = location.getLongitude();
    47.         lat = location.getLatitude();
    48.         LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient,  this);
    49.     }
    50.  
    51.  
    52.  
    53.     @Override
    54.     public void onConnectionFailed(ConnectionResult connectionResult) {
    55.  
    56.     }
     

    Advertisement

    #1 Amit Laloush, Apr 14, 2016
    Last edited: Apr 14, 2016
  2. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,866
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,866
    11,495
    1,988
    Male
    Software developer
    South West of England
    Absolutely impossible to answer without seeing code, and a full stack trace.
     
  3. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,866
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,866
    11,495
    1,988
    Male
    Software developer
    South West of England
  4. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,866
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,866
    11,495
    1,988
    Male
    Software developer
    South West of England
    Thanks for the code, but you need to also give the full stack trace
     
  5. Amit Laloush

    Amit Laloush Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    2
    Joined:
    Apr 14, 2016

    Apr 14, 2016
    2
    0
    5
    Male
    I'm only have the main activity that build a findLocation object in onCreate method
     
  6. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,866
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,866
    11,495
    1,988
    Male
    Software developer
    South West of England
    Still need to see the stack trace from your Logcat output to help you
     

Share This Page

Loading...