2011-12-15 2 views
1

위도와 경도를 반환하는 Android 앱을 작성 중이지만 위치는 Null 값을 포함합니다.위치가 NULL을 반환합니다.

이유를 볼 수 있는지 확인하십시오. 하루 종일 저를 괴롭혔습니다. 아래 코드 :

는 GPS 적 장치가 부팅 된 이후 위치를 확보하지 않은 경우
public class example extends Activity { 

    public static double latitude; 
    public static double longitude; 
    LocationManager lm; 
    LocationListener ll; 
    Location location; 


    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.questions); 


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

     ll = new MyLocationListener(); 

     lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll); 
     //location = lm.getLastKnownLocation(lm.getBestProvider(criteria, true)); 

     Button b = (Button) findViewById(R.id.button2); 
     b.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); 
      int index1 = provider.indexOf("gps"); 
      if(index1 < 0) 
       // gps not enabled, call msgbox  
       showMsgBox("GPS is off", "GPS is off. Turn it on?", "Turn on"); 
      else 
      areWeThereYet(); }}); 
} 

    private void areWeThereYet() 
    { 
     if(weAreThere()) 
     { 
      toastMsg("in correct place");  
     } 
     else if(location!=null) 
      toastMsg("not there yet"); 
    } 

private boolean weAreThere() { 

    location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

    if (location!=null) 
    { 
     longitude = location.getLongitude(); 
     latitude = location.getLatitude(); 
     toastMsg(latitude + " " + longitude); 
     return inCorrectPlace(question); 
    } 
    else 
    { 
     toastMsg("Location not ready yet"); 
     return false; 
    } 
} 

private void toastMsg(String msg) { 
     Toast toast = Toast.makeText(this, msg, 2000); 
     toast.setGravity(Gravity.BOTTOM, 0, 0); 
      toast.show(); 
} 
} 

답변

3

, 위치 객체는 null가됩니다. 당신이 할 수있는 한 가지는 GPS 위치와 네트워크 위치를 얻으려고 시도하는 것입니다. 둘 중 어느 것이 좋고 (또는 어느 쪽이 더 낫지) 그 위치를 사용하는지 확인하십시오.

2

에뮬레이터를 사용하는 경우 here에서 위치를 제공하도록 에뮬레이터를 설정하는 방법을 참조하십시오. 장치에서 테스트 중이라면 GPS 위치가 없었기 때문일 수 있습니다. 앱을 테스트하기 전에 Google지도 애플리케이션을 사용해보세요.