我正在使用GPS在Android应用中获取我的位置。但是当我创建一个LocationManager时,它返回空指针异常。即使在androidMainFest.xml和app上设置了权限,它仍会返回以下异常。任何人都可以提前帮助。谢谢!
代码:
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();
}
}
例外:
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" />
答案 0 :(得分:1)
LocationManager
不会返回null
。错误是您传递了null
LocationListener
。
您需要先将loclist
初始化,然后再将其传递给requestLocationUpdates
答案 1 :(得分:0)
您声明locList
并将其传递给lm.requestLocationUpdates(bestProvider, 2000, 0, (android.location.LocationListener) locList);
方法。尽管如此,它看起来并不像是在初始化它。
这就是它告诉你的异常,你传递的是null,它正在期待一个监听器。
您可以删除locList
声明并尝试进行以下更改:
变化
lm.requestLocationUpdates(bestProvider, 2000, 0, (android.location.LocationListener) locList);
到
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.
*/
}
});