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

有很多类似的问题,但我没有找到任何解决我的问题.
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();
        }
    }
上一篇:线程中的异常,java.lang.NullPointerException


下一篇:android – v7 RecyclerView NullPointerException AccessibilityDelegateCompat.getBridge()