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

Android Google Maps V2当前位置纬度经度NullPointerException,第1张

概述有很多类似的问题,但我没有找到任何解决我的问题.setUpMap方法是:privatevoidsetUpMap(){BitmapDescriptoriconm=BitmapDescriptorFactory.fromResource(R.drawable.m);BitmapDescriptoriconc=BitmapDescriptorFactory.fromResource(R.drawable.c);//En

有很多类似的问题,但我没有找到任何解决我的问题.
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全部内容,希望文章能够帮你解决Android Google Maps V2当前位置纬度经度NullPointerException所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1107808.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存