-1

ACCESS_COARSE_LOCATION和ACCESS_FINE_LOCATON都包含在清單中。還包括在活動是:導致空指針的GPS精度不足

import android.location.Location; 
import android.location.LocationManager; 

我對導航抽屜監聽器與下面的類

private class DrawerItemClickListener implements ListView.OnItemClickListener { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     if (location != null) 
     { 
      final double longitude = location.getLongitude(); 
      final double latitude = location.getLatitude(); 
     } 

    selectItem(position); 
    } 
} 

由此看來,location = null,因此將無法在location.getLongitude();。我當然可以避免這個與if (location != null),但我想找到latlong

爲什麼位置始終爲空?

+1

您的目標是API 23還是更高?你在哪裏[在運行時請求許可](https://developer.android.com/training/permissions/requesting.html)? – ianhanniballake

回答

1

在硬件方面尋找位置不是一件容易的事。你不能告訴設備我想知道我的位置,所以現在給我吧!如果外面有清晰的可見度,它需要觀察GPS衛星的一些數據,否則它會使用其他可用的選項,如WiFi。直到您的設備拿出一個有效的位置,您應該等待。我建議您觀察onLocationChanged事件並在發生此事件時更新您的列表視圖。此外它是很好的使用null條件,直到它檢測到你的位置。

+0

過去幾天我們遭遇了暴風雪。這可能是原因嗎? – Iorek

+0

即使是最好的情況下,如果你直接使用位置,你最有可能得到空數據。所以這就是爲什麼你應該等待onLocationChanged事件 – uguboz

+0

是的,解決方案是http://stackoverflow.com/questions/9873190/my-current-location-always-returns-null-how-cani-i-fix-this。 Mods跳槍關閉它 – Iorek