gpt4 book ai didi

java - Android 位置始终为空

转载 作者:行者123 更新时间:2023-11-30 04:25:55 25 4
gpt4 key购买 nike

我的应用程序可以找到位置,但我更改了一些代码,现在位置总是返回 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();
}
}

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

谢谢,

最佳答案

它为空,因为当您第一次在 onCreate 中启动您的 Activity 时,它还没有准备好。当您最终通过 onLocationChanged 获得位置更新时,您在 Clue 类上设置了纬度/经度,仅此而已……没有什么会重新调用您的 areWeThereYet 方法。您需要在更新位置后调用该方法。

关于java - Android 位置始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8524934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com