我知道这个问题已经有几个答案,但是很不幸,我是一个初学者,我找不到适合该问题的正确解决方案。 getLastKnowPosition
方法不返回任何内容。我该如何解决?我需要设备的当前位置。
编辑我现在正在尝试评论中发布的解决方案。
代码
public class IntellySearch extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private LocationManager locationManager;
private double posLat;
private double posLng;
private LatLng position;
private Marker mPosition;
private int numero = 1;
private FusedLocationProviderClient mFusedLocationClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intelly_search);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
mFusedLocationClient.getLastLocation().addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null){
posLat = location.getLatitude();
posLng = location.getLongitude();
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d("IntActivity", "Error getting last GPS location");
e.printStackTrace();
}
});
startGPS();
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng duomo = new LatLng(43.773251, 11.255474);
//mMap.moveCamera(CameraUpdateFactory.newLatLng(duomo));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(duomo, 18f));
}
public void startGPS() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 5);
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 5, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
posLat = location.getLatitude();
posLng = location.getLongitude();
position = new LatLng(posLat, posLng);
if (mPosition != null) {
mPosition.remove();
}
mPosition = mMap.addMarker(new MarkerOptions().position(position).title("Your position"));
if (numero < 2) {
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 18));
numero = 30;
}
mMap.moveCamera(CameraUpdateFactory.newLatLng(position));
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
});
}
stacktrace引用两行代码:
首先
public void onLocationChanged(Location location) {
posLat = location.getLatitude();
posLng = location.getLongitude();
position = new LatLng(posLat, posLng);
if (mPosition != null) {
mPosition.remove();
}
第二
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng duomo = new LatLng(43.773251, 11.255474);
//mMap.moveCamera(CameraUpdateFactory.newLatLng(duomo));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(duomo, 18f));
}
堆栈
2019-01-31 11:10:30.269 7441-7441/com.icloud.andreadimartino.bellini I/Google Maps Android API: Google Play services client version: 12211000
2019-01-31 11:10:30.275 7441-7441/com.icloud.andreadimartino.bellini I/Google Maps Android API: Google Play services package version: 13280022
2019-01-31 11:10:30.311 1368-1411/? W/audio_hw_generic: Hardware backing HAL too slow, could only write 0 of 720 frames
2019-01-31 11:10:30.463 7441-7441/com.icloud.andreadimartino.bellini E/art: The String#value field is not present on Android versions >= 6.0
2019-01-31 11:10:30.896 7441-7441/com.icloud.andreadimartino.bellini E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.icloud.andreadimartino.bellini, PID: 7441
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.icloud.andreadimartino.bellini/com.icloud.andreadimartino.bellini.IntellySearch}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.location.LocationManager.requestLocationUpdates(java.lang.String, long, float, android.location.LocationListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.location.LocationManager.requestLocationUpdates(java.lang.String, long, float, android.location.LocationListener)' on a null object reference
at com.icloud.andreadimartino.bellini.IntellySearch.startGPS(IntellySearch.java:104)
at com.icloud.andreadimartino.bellini.IntellySearch.onCreate(IntellySearch.java:79)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
我如何解决?