-1

I'm using GPS to get my location in an android app. But when I create a LocationManager it returns null pointer exception. Even though the permissions are set on the androidMainFest.xml and also for the app, still it returns the following exception. Can anyone help in advance. Thank You!

Code :

package com.example.pavsaranga.scat;

import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;

import com.google.android.gms.location.LocationListener;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class Map extends FragmentActivity implements OnMapReadyCallback, LocationListener {

    private GoogleMap mMap;
    double longitude, latitude;
    String bestProvider;
    LocationManager lm;
    Location location;
    Criteria criteria;
    LocationListener locList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        mMap.getUiSettings().setZoomGesturesEnabled(true);
        try {
            boolean permissionGranted = ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;
            if (permissionGranted) {
                setMyLocation();
            } else {
                ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 200);
            }
        } catch(Exception e){
            e.printStackTrace();
        }
    }

    private void setMyLocation() {
        try {
            lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
            if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(Map.this, "Please Grant Permission.", Toast.LENGTH_SHORT).show();
            } else {
                criteria = new Criteria();
                bestProvider = String.valueOf(lm.getBestProvider(criteria, true)).toString();
                location = lm.getLastKnownLocation(bestProvider);
                if (location != null) {
                    latitude = location.getLatitude();
                    longitude = location.getLongitude();
                }
                else{
                    lm.requestLocationUpdates(bestProvider, 2000, 0, (android.location.LocationListener) locList);
                }
                LatLng myLocation = new LatLng(latitude, longitude);
                mMap.addMarker(new MarkerOptions().position(myLocation).title("You Are Here!"));
                mMap.moveCamera(CameraUpdateFactory.newLatLng(myLocation));
            }
        } catch(Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case 200: {
                if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(Map.this, "Thank You, Permission Granted!.", Toast.LENGTH_SHORT).show();
                    setMyLocation();
                }
            }
        }
    }

    @Override
    public void onLocationChanged(Location location) {
        latitude = location.getLatitude();
        longitude = location.getLongitude();
    }
}

Exception :

java.lang.IllegalArgumentException: invalid listener: null

MainFest.xml

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

2 Answers2

1

LocationManager isn't returning null. The error is that you are passing a null LocationListener.

You need to initialize loclist before passing it to requestLocationUpdates

Bret Deasy
  • 820
  • 6
  • 14
0

You are declaring locList and passing it in to the lm.requestLocationUpdates(bestProvider, 2000, 0, (android.location.LocationListener) locList); method. It doesn't look like you are initializing it though. That is what the exception it telling you, you are passing in null where it is expecting a listener.

You can remove the locList declaration and try making the following change:

change

lm.requestLocationUpdates(bestProvider, 2000, 0, (android.location.LocationListener) locList);

to

lm.requestLocationUpdates(bestProvider, 2000, 0, new LocationListener(){
    // @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        /*
        * Called when the provider status changes. 
        * This method is called when a provider is unable to fetch a location 
        * or if the provider has recently become available after a period of unavailability.
        */
    }

    // @Override
    public void onLocationChanged(Location location) {
        /*
        * Called when the location has changed. 
        */
    }
});
SeanKelleyx
  • 1,155
  • 13
  • 15