J'utilise le GPS pour obtenir ma position dans une application Android. Mais quand je crée un LocationManager, il renvoie une exception de pointeur nul. Même si les autorisations sont définies sur androidMainFest.xml et également pour l'application, elle renvoie quand même l'exception suivante. Quelqu'un peut-il aider à l'avance. Je vous remercie!LocationManager renvoie null
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" />
Vous testez cela sur un périphérique matériel ou AVD? –
testé sur mon téléphone –