Android的位置总是空 [英] Android location always null

查看:154
本文介绍了Android的位置总是空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的应用程序查找位置的工作,但我改变了一些code和现在所在的位置始终返回null。惭愧,我不知道什么code我就变了,但我不能似乎得到它的工作。

I had my application to find location working, but I changed some code and now the location always returns null. Ashamedly I am not sure what code I had changed, but I just cant seem to get it working.

有人能看到为什么返回null?我一直在做这一切的一天,没有快乐。
 低于code:

Can anyone see why this returns null? I have been working on this all day with no joy. code below:

....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();
    }
}

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

I have added all permissions and simplified my code so you can all see what's going on.

谢谢,

推荐答案

这是null,因为当你第一次启动的onCreate您的活动是还没有准备好。当你最终获得通过onLocationChanged位置更新,你设置的纬度/经度上的线索类,但仅此而已...什么都没有重新调用你的 areWeThereYet 方法。你需要调用该方法更新自己的位置了。

It's null because when you first start your activity in onCreate it isn't ready yet. When you eventually get the location update via onLocationChanged, you set the lat/lon on your Clue class but that's it... nothing re-calls your areWeThereYet method. You'd need to call that method after updating your location.

这篇关于Android的位置总是空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆