2011-12-15 93 views
1

我让我的应用程序找到位置工作,但我更改了一些代码,现在位置总是返回null。惭愧的是,我不确定我改变了哪些代码,但我似乎无法让它工作。Android位置始终为空

任何人都可以看到为什么这返回null?我一整天都在努力,没有快乐。下面 代码:

....imports.... 

public class Clue extends Activity { 

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

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    setContentView(R.layout.questions); 

    criteria = new Criteria(); 
    criteria.setAccuracy(Criteria.ACCURACY_FINE); 
    lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    ll = new MyLocationListener(); 
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll); 

areWeThereYet(); 
} 

    private void areWeThereYet() 
    { 
     if(weAreThere()) 
     { 
     showMsgBox("There"); 
    } 
    else if(location!=null) 
     toastMsg("not there"); 
     } 

    private boolean weAreThere() { 

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

private Location getLocation() { 
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll); 
     return lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
} 

} 

public class MyLocationListener implements LocationListener 
{ 
    public void onLocationChanged(Location loc) 
    {   
     Clue.longitude = loc.getLongitude(); 
     Clue.latitude = loc.getLatitude(); 
    } 
} 

我添加了所有的权限和简化我的代码,所以你都可以看到发生了什么事情。

感谢,

+0

只是对未来的建议:把你的代码放在版本控制中! – 2011-12-15 18:37:22

+0

我通常这样做,这是一个小项目,所以我没有打扰:希望我现在做了! – 2011-12-15 18:39:35

回答

1

这是null,因为当你第一次启动的onCreate您的活动是还没有准备好。当你最终通过onLocationChanged获得位置更新时,你可以在你的Clue类上设置纬度/经度,但就是这样......没有什么可以重新调用你的areWeThereYet方法。更新您的位置后,您需要调用该方法。