gpt4 book ai didi

Android Google Maps V2 当前位置经纬度 NullPointerException

转载 作者:太空宇宙 更新时间:2023-11-03 12:51:57 25 4
gpt4 key购买 nike

有很多类似的问题,但我没有找到解决我的问题的方法。setUpMap 方法是:

private void setUpMap() {
BitmapDescriptor iconm = BitmapDescriptorFactory.fromResource(R.drawable.m);
BitmapDescriptor iconc = BitmapDescriptorFactory.fromResource(R.drawable.c);
// Enable MyLocation Layer of Google Map
mMap.setMyLocationEnabled(true);
// Get LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Create a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Get the name of the best provider
String provider;
provider = locationManager.getBestProvider(criteria,true);
// Get Current Location
Location myLocation = locationManager.getLastKnownLocation(provider);
// getting GPS status
boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
boolean isNWEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

if (!isNWEnabled)
{
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setMessage(getString(R.string.askposition) );
dialog.setPositiveButton(getString(R.string.settings), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
// TODO Auto-generated method stub
startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 100);
}
});
dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
// TODO Auto-generated method stub
}
});
dialog.show();
// no network provider is enabled
Log.e("Current Location", "Current Lat Lng is Null");
}
else
{
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled){
if (myLocation == null) {
if (locationManager != null) {
myLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double latitude = myLocation.getLatitude();
double longitude = myLocation.getLongitude();
LatLng latlng = new LatLng(latitude, longitude);
//myLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
//double latitude = myLocation.getLatitude();
//double longitude = myLocation.getLongitude();
//String lati = String.valueOf(latitude);
//String longi = String.valueOf(longitude);
// Create a LatLng object for the current location
//LatLng latlng = new LatLng(latitude, longitude);
// set map type
//mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
//CameraPosition cameraPosition = new CameraPosition.Builder().target(latlng).zoom(12).build();

//mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

// Show the current location in Google Map
// mMap.moveCamera(newLatLng(latlng));
// Zoom in the Google Map
//mMap.animateCamera(CameraUpdateFactory.zoomTo(12));
}
}
}
else {
// First get location from Network Provider
if (isNWEnabled) {
if (myLocation == null) {
if (locationManager != null) {
myLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
double latitude = myLocation.getLatitude();
double longitude = myLocation.getLongitude();
LatLng latlng = new LatLng(latitude, longitude);
//double latitude = myLocation.getLatitude();
//double longitude = myLocation.getLongitude();

// Create a LatLng object for the current location
//LatLng latlng = new LatLng(latitude, longitude);
// set map type
//mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

// Zoom in the Google Map
//CameraPosition cameraPosition = new CameraPosition.Builder().target(latlng).zoom(12).build();

//mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
//mMap.animateCamera(CameraUpdateFactory.zoomTo(12));
// Show the current location in Google Map
//mMap.moveCamera(newLatLng(latlng));
}
}
}}
}

double latitude = myLocation.getLatitude();
double longitude = myLocation.getLongitude();
LatLng latlng = new LatLng(latitude, longitude);

CameraPosition cameraPosition = new CameraPosition.Builder().target(latlng).zoom(11).build();
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

当我第一次将应用程序从 Android Studio 运行到我的设备时,它工作正常,如果我关闭并重新启动应用程序,它也工作正常。但是当我重新启动我的智能手机时,它给了我 NullPointerException @ double latitude = myLocation.getLatitude();
从那一刻起,它总是崩溃。我该如何解决?注释代码行大约是几次尝试。完全一样的问题。任何帮助将非常感激。提前致谢。

最佳答案

我认为您的问题的解决方法是使用 PASSIVE_PROVIDER .

示例代码:

    LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);
myLocation = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
double longitude = myLocation.getLongitude();
double latitude = myLocation.getLatitude();

private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
longitude = location.getLongitude();
latitude = location.getLatitude();
}
}

关于Android Google Maps V2 当前位置经纬度 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28877835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com