Android 位置始终为空

发布于 2024-12-21 10:59:01 字数 1658 浏览 2 评论 0原文

我的应用程序可以找到位置,但我更改了一些代码,现在位置始终返回 null。遗憾的是,我不确定我更改了哪些代码,但我似乎无法让它工作。

谁能明白为什么这返回 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();
    }
}

我已经添加了所有权限并简化了我的代码,以便你们都可以看到发生了什么。

谢谢,

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.

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

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

Thanks,

Ben

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆悲凉 2024-12-28 10:59:01

它为空,因为当您第一次在 onCreate 中启动 Activity 时,它尚未准备好。当您最终通过 onLocationChanged 获得位置更新时,您在 Clue 类上设置了纬度/经度,但仅此而已......没有任何东西重新调用您的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文