getLastKnownLocation返回NULL [英] getLastKnownLocation returning NULL

查看:110
本文介绍了getLastKnownLocation返回NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚得到这个工作,但是当我关掉我的WiFi,看看我是否可以用我的GPS它现在把我一个空指针错误得到更准确的定位我不明白为什么。

I had just got this to work but when I turned off my Wifi to see if I could get a more accurate location using my GPS it's now bringing me a NullPointer error and I can't see why.

下面code被称为第一;

The below code gets called first;

 public void onConnected(Bundle dataBundle) {
        connected = true;
        //Request an update from the location client to get current Location details
        mLocationClient.requestLocationUpdates(mLocationRequest,this);
        Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();
    }

然后限嗣继承调用这个方法

Which then entails this method being called

public void onLocationChanged(android.location.Location location) {
        // Report to the UI that the location was updated
        String msg = "Updated Location: " +
                Double.toString(location.getLatitude()) + "," +
                Double.toString(location.getLongitude());
        if(tmp != location.getLatitude())
        {
            Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
        }
        tmp = location.getLatitude();
    }

您可以看到我有2个烤面包那里,他们回来精细与实际的GPS坐标,但是当我打电话下面的code崩溃了在新的位置行 getLastKnownLocation

You can see I have 2 toasts there and they come back fine with the actual GPS coordinates but when I call the below code it crashes out on the new location line getLastKnownLocation

public String getLocation()
    {
        // Get the location manager
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        String bestProvider = locationManager.getBestProvider(criteria, false);
        android.location.Location location = locationManager.getLastKnownLocation(bestProvider);
        Double lat,lon;
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = new Date();
        try {
            lat = location.getLatitude ();
            lon = location.getLongitude ();
            Toast.makeText(this,""+lat.toString()+"-"+lon.toString(),Toast.LENGTH_SHORT).show();
            return new LatLng(lat, lon).toString();
        }
        catch (NullPointerException e){
            Toast.makeText(this,"HELL-NO",Toast.LENGTH_SHORT).show();
            Log.e("HELL-NO","n",e);
            e.printStackTrace();
            return null;
        }
    }

我只是不明白为什么当我尝试检索位置,这似乎已经提取了 onLocationChanged 方法,它只是来了一个空指针。

I just don't get why when I try to retrieve the location, which seems to have been retrieved in the onLocationChanged method, it just comes up with a nullpointer.

推荐答案

你混合新和旧位置的API在一起的时候,你不应该。

you are mixing the new and the old location API's together when you shouldnt.

要获得最后已知位置所有需要做的就是调用

to get the last known location all your have to do is call

mLocationClient.getLastLocation();

在位置服务相连接。

读了如何使用新的定位API

read how to use the new location API

<一个href="http://developer.android.com/training/location/retrieve-current.html#GetLocation">http://developer.android.com/training/location/retrieve-current.html#GetLocation

这篇关于getLastKnownLocation返回NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆