-1

ACCESS_COARSE_LOCATION 및 ACCESS_FINE_LOCATON이 모두 매니페스트에 포함되었습니다. 또한 활동에 포함됩니다 :null 포인터가 발생하는 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이 필요합니다.

위치가 항상 null입니까?

+1

API 23 이상을 타겟팅합니까? [런타임에 권한 요청] (https://developer.android.com/training/permissions/requesting.html)을 어디에서합니까? – ianhanniballake

답변

1

위치 찾기는 하드웨어 측면에서 쉬운 작업이 아닙니다. 당신은 내 위치를 알고 싶어서 장치를 말할 수 없으니 지금 당장 나에게 줘! 바깥 시야가 명확하지 않으면 GPS 위성의 일부 데이터를 관찰해야합니다. 그렇지 않으면 Wi-Fi와 같은 다른 사용 가능한 옵션이 사용됩니다. 기기가 유효한 위치를 찾을 때까지 기다려야합니다. 이 이벤트가 발생하면 onLocationChanged 이벤트를 관찰하고 목록보기를 업데이트하는 것이 좋습니다. 또한 위치를 감지 할 때까지 null 조건을 사용하는 것이 좋습니다.

+0

우리는 지난 며칠간 폭풍이 올랐습니다. 이것이 원인 일 수 있습니까? – Iorek

+0

최상의 경우에도 u 위치를 직접 사용하면 대부분 null 데이터를 얻게됩니다. 그래서 당신은 onLocationChanged 이벤트를 기다려야합니다. – uguboz

+0

예, 해결책은 http://stackoverflow.com/questions/9873190/my-current-location-always-returns-null-how-can-i-fix-this입니다. 총을 점프하는 MOD가 – Iorek