繁体   English   中英

Android位置始终为null

[英]Android location always null

我让我的应用程序找到了可以正常工作的位置,但是我更改了一些代码,现在位置始终返回null。 令人ham愧的是,我不确定自己更改了哪些代码,但似乎无法正常工作。

谁能看到为什么返回空值? 我整天都在工作,没有任何喜悦。 下面的代码:

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

我已经添加了所有权限并简化了代码,因此您都可以看到正在发生的事情。

谢谢,

这是空的,因为当您第一次在onCreate中开始活动时,它尚未准备好。 当您最终通过onLocationChanged获得位置更新时,可以在Clue类上设置纬度/经度,仅此而已...没有任何东西可以调用您的areWeThereYet方法。 更新位置后,您需要调用该方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM