2015-02-02 2 views
1

Google지도를 사용하여 현재 위치를 표시하려고합니다. '위치'가 전화 (갤럭시 s4)에서 켜져있는 한 잘 실행됩니다. 하지만 전화로 '위치 정보'를 끄면 충돌이 발생합니다.위치 앱이 손상되는 이유

로그 캣은 공급자를 말하고 다음 줄에 널 (null)입니다

Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false)); 

내가 대신 응용 프로그램 충돌의 "위치에 회전을 말하는 메시지를 팝업합니다.에 대한 간단한 & 좋은 방법 일 것입니다 무슨 그렇게

코드 :

public class MapsActivity extends FragmentActivity { 

    private GoogleMap mMap; // Might be null if Google Play services APK is not available. 
    private Marker marker; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_maps); 
     setUpMapIfNeeded(); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     setUpMapIfNeeded(); 
    } 


    private void setUpMapIfNeeded() { 
     // Do a null check to confirm that we have not already instantiated the map. 
     if (mMap == null) { 
      // Try to obtain the map from the SupportMapFragment. 
      mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)) 
        .getMap(); 
      // Check if we were successful in obtaining the map. 
      if (mMap != null) { 
       setUpMap(); 
      } 
     } 
    } 


    private void setUpMap() { 
     mMap.addMarker(new MarkerOptions().position(new LatLng(32, 270)).title("Marker")); 
     mMap.setMyLocationEnabled(true); 


     LocationManager locationManager= (LocationManager) getSystemService(Context.LOCATION_SERVICE); 


      Criteria criteria = new Criteria(); 

      Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false)); 

      mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 14)); 


     } 
} 
+0

정확히 스택 추적이란 무엇입니까? – stkent

+0

어쩌면 당신은 http://stackoverflow.com/questions/3470693/android-get-location-or-prompt-to-enable-location-service-if-disabled – k3v1n4ud3

+0

잘 모르겠습니다. 한 달 전에 나는 자바의 기본조차 몰랐다. 여기서 내 목표를 달성하기 위해 통과해야 할 주제는 무엇입니까? –

답변

0

이 호출 locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false)); 반환 null 위치 아가 경우 e는 사용 불가능합니다. location 변수가 null인지 확인하지 않았기 때문에 앱이 다운되었습니다.

관련 문제