Android 位置始终为空
我的应用程序可以找到位置,但我更改了一些代码,现在位置始终返回 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它为空,因为当您第一次在 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.