2011-12-15 28 views
1

私は緯度と経度を返すAndroidアプリを作成していますが、場所はNull値を保持しています。場所がNULLを返します

私はいつも私を悩まされている理由を見ることができるかどうか見てください。コード:

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

デバイスが起動してからGPSがロケーションを取得していない場合、ロケーションオブジェクトはnullになります。あなたができることの1つは、GPSの場所とネットワークの場所を取得し、どちらかが良い(またはどちらが良いか)かどうかを確認し、その場所を使用するかどうかを確認することです。

2

エミュレータを使用している場合は、hereを参照して、エミュレータで場所を指定するように設定することをおすすめします。あなたのデバイス上でそれをテストしている場合、あなたはそれの上にGPSの場所を持っていないためかもしれません。アプリをテストする前にGoogleマップアプリケーションを使用してみてください。

関連する問題