Android GPS 错误

发布于 2024-12-19 19:35:25 字数 2537 浏览 1 评论 0原文

当我尝试通过 GPS 或网络获取当前位置时,我的应用程序出现 GPS 错误

private static LocationManager lm;
private static LocationListener locationListener;
private static Location posicion;   
private double latitud;
private double longitud;

posicion = ObtenPosicion();       
        latitud = posicion.getLatitude();
        longitud= posicion.getLongitude();

        //Me centro en el mapa
        final GeoPoint yo = new GeoPoint((int)(latitud),(int)(longitud));   

 public Location ObtenPosicion(){
     if (lm == null)
        {
            lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);    

            locationListener = new LocListener();

            if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER))
            {
                lm.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER, 
                    LOCATION_UPDATE_TIME, 
                    LOCATION_UPDATE_DISTANCE, 
                    locationListener);

                posicion = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            }

            if (lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
            {
                        lm.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER, 
                        LOCATION_UPDATE_TIME, 
                        LOCATION_UPDATE_DISTANCE,  
                        locationListener);

                posicion = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            }
        }
        return posicion;
    }



 private class LocListener implements LocationListener 
    {
        @Override
        public void onLocationChanged(Location loc) {
           posicion.set(loc);
        }

        @Override
        public void onProviderDisabled(String provider) {

        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onStatusChanged(String provider, int status, 
            Bundle extras) {
        }


    }  

    public static Location getCurrentLocation ()
    {
        return posicion;
    }

    public void stopLocationListening ()
    {
        if (lm != null)
            lm.removeUpdates(locationListener);

    }

该错误是一行中的空指针异常:

12-06 17:04:25.418: E/AndroidRuntime(4744): java.lang.NullPointerException
12-06 17:04:25.418: E/AndroidRuntime(4744):     at com.rbrlnx.lugares.editarLugar$LocListener.onLocationChanged(editarLugar.java:215)

我尝试使用不同的代码,但这是不可能的..

I have a GPS error in my app when I am trying to get my current location, by gps or by network

private static LocationManager lm;
private static LocationListener locationListener;
private static Location posicion;   
private double latitud;
private double longitud;

posicion = ObtenPosicion();       
        latitud = posicion.getLatitude();
        longitud= posicion.getLongitude();

        //Me centro en el mapa
        final GeoPoint yo = new GeoPoint((int)(latitud),(int)(longitud));   

 public Location ObtenPosicion(){
     if (lm == null)
        {
            lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);    

            locationListener = new LocListener();

            if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER))
            {
                lm.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER, 
                    LOCATION_UPDATE_TIME, 
                    LOCATION_UPDATE_DISTANCE, 
                    locationListener);

                posicion = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            }

            if (lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
            {
                        lm.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER, 
                        LOCATION_UPDATE_TIME, 
                        LOCATION_UPDATE_DISTANCE,  
                        locationListener);

                posicion = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            }
        }
        return posicion;
    }



 private class LocListener implements LocationListener 
    {
        @Override
        public void onLocationChanged(Location loc) {
           posicion.set(loc);
        }

        @Override
        public void onProviderDisabled(String provider) {

        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onStatusChanged(String provider, int status, 
            Bundle extras) {
        }


    }  

    public static Location getCurrentLocation ()
    {
        return posicion;
    }

    public void stopLocationListening ()
    {
        if (lm != null)
            lm.removeUpdates(locationListener);

    }

The error is a nullpointerexception in a line :

12-06 17:04:25.418: E/AndroidRuntime(4744): java.lang.NullPointerException
12-06 17:04:25.418: E/AndroidRuntime(4744):     at com.rbrlnx.lugares.editarLugar$LocListener.onLocationChanged(editarLugar.java:215)

I tried with a different codes but its me impossible..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

能怎样 2024-12-26 19:35:25

您的问题在于您没有分配“posicio”对象。您只需将其声明为静态即可。因此,当您在 onLocationChanged 回调中从系统获取回调时,您尝试使用 null 对象。

在 onCreate 方法中,您需要使用以下方法分配和初始化位置:

posicion = new Location();

Your issue is with the fact that you did not allocate a 'posicio' object. You simply declared it as static. So, when you get the callback from the system in the onLocationChanged callback you try to use a null object.

In your onCreate method, you need to allocate and initialize the posicion with:

posicion = new Location();
黑凤梨 2024-12-26 19:35:25

posicion 变量在这里为 null.... 初始化它尝试两个中的任何一个/两者

onCreate() 方法中,使用默认值初始化 posicion。和/或在必要时使用 try/catch 在适当的位置调用 ObtenPosicion()

The posicion variable is null here.... Initialize it try any/both of the two

In onCreate() method, initialize posicion with a default value. And/or than call ObtenPosicion() at appropriate place with try/catch if necessary

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文