I'm working with the map to my app and so I have here a weird error
message that it says java.lang.SecurityException: "GPS" location provider requires ACCESS_FINE_LOCATION permission.
every time I launch the app and crashes due to this error, though I already have a permission checker.
Here is my code:
public class FragmentHome extends Fragment implements OnMapReadyCallback {
/*
Set up's
*/
private static final String TAG = "FragmentHome";
/*
Pallete
*/
private MapView mapView;
private GoogleMap gMap;
private static final String MAP_VIEW_BUNDLE_KEY = "SOME_KEY";
@SuppressLint("SetJavaScriptEnabled")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view_fragmentInflate = inflater.inflate(R.layout.fragment_fragment_home, container, false);
mapView = (MapView) view_fragmentInflate.findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
return view_fragmentInflate;
}
@Override
public void onMapReady(GoogleMap googleMap) {
getUserLocation();
}
private void getUserLocation() {
LocationManager locationManager = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Log.i(TAG, "onLocationChanged: " + location);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (getContext().checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
getContext().checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// Activity#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for Activity#requestPermissions for more details.
requestPermissions(new String[] {
Manifest.permission.ACCESS_FINE_LOCATION
}, 1);
}
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
3000, 0, locationListener);
}
}
I really do not know where I went wrong from this line of code, to keep my app crashing and give me that error.
Update your code with bellow code may solve your problem
public class FragmentHome extends Fragment implements OnMapReadyCallback {
/*
Set up's
*/
private static final String TAG = "FragmentHome";
/*
Pallete
*/
private MapView mapView;
private GoogleMap gMap;
private static final String MAP_VIEW_BUNDLE_KEY = "AIzaSyDWL-JNHiCXvQefgFh1BdaAflJTveSrHJo";
@SuppressLint("SetJavaScriptEnabled")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view_fragmentInflate = inflater.inflate(R.layout.fragment_fragment_home, container, false);
mapView = (MapView) view_fragmentInflate.findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
return view_fragmentInflate;
}
boolean isMapReady;
@Override
public void onMapReady(GoogleMap googleMap) {
isMapReady=true;
getUserLocation();
}
private void getUserLocation() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ActivityCompat.checkSelfPermission(getContext(),Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(getContext(),Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// Activity#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for Activity#requestPermissions for more details.
requestPermissions(new String[]{
Manifest.permission.ACCESS_FINE_LOCATION
}, 1);
return;
}
}
LocationManager locationManager = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Log.i(TAG, "onLocationChanged: " + location);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
3000, 0, locationListener);
}
}
Now override onRequestPermissionsResult()
and check if result is granted and isMapReady flag true then call getUserLocation()
.