gpt4 book ai didi

java - getLatitude 和 getLongitude 抛出 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 07:48:01 25 4
gpt4 key购买 nike

请您解释一下为什么 getLongitude 和 getLatitude 返回 NullPointerException 吗?

事实是问题已经解决了,但是有一件事困扰着我,因为我们的论文协调员可能会问这个问题。情况是我必须获取用户当前的坐标(纬度和经度)。我决定使用 FusedLocationClient.getLastLocation() 来获取坐标。

mFusedLocationClient = getFusedLocationProviderClient(this);

//app already crashes once the line below is executed

mFusedLocationClient.getLastLocation().addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if(location != null){
//myLocation is an instance variable of this class
myLocation = location;
}else{
Toast.makeText(PlaceCategoriesActivity.this, "Location fetch failed!", Toast.LENGTH_SHORT).show();
}
}
});

latitude = myLocation.getLatitude(); //this line throws NullPointerException
longitude = myLocation.getLongitude();

该应用程序似乎编译正常,但随后崩溃了,通过在 myLocation.getLatiude() 和 myLocation.getLongititude() 上添加调试断点进行检查,结果发现此方法调用了空指针异常,这对我来说很奇怪,因为 myLocation 已经引用了 addOnSuccessListener 的 onSuccess 方法带来的 location 对象。

我已经通过移动最后两行代码并将其放入监听器中解决了问题,如下所示:

        mFusedLocationClient = getFusedLocationProviderClient(this);

mFusedLocationClient.getLastLocation().addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if(location != null){
myLocation = location;

//moved the 2 lines of code and it now works
latitude = myLocation.getLatitude();
longitude = myLocation.getLongitude();
}else{
Toast.makeText(PlaceCategoriesActivity.this, "Location fetch failed!", Toast.LENGTH_SHORT).show();
}
}
});

//Toast returns a value of 0 on latitude and 0 on longitude
Toast.makeText(this, "Lat: " + latitude + "Long: + " + longitude, Toast.LENGTH_SHORT).show();

有趣的是,Toast 上显示的经度和纬度的值都是 0。

最佳答案

which for me is weird because myLocation already has referenced the location object brought by the onSuccess method of the addOnSuccessListener

让我们简化您的第一个代码 list :

mFusedLocationClient = getFusedLocationProviderClient(this);

latitude = myLocation.getLatitude(); //this line throws NullPointerException
longitude = myLocation.getLongitude();

在这里,myLocation尚未被赋值。如果您还没有给 myLocation 赋值在前面的行中,myLocation将是null ,您将得到 NullPointerException .

现在,让我们恢复该代码 fragment 的一些行:

mFusedLocationClient = getFusedLocationProviderClient(this);

//app already crashes once the line below is executed

mFusedLocationClient.getLastLocation().addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
}
});

latitude = myLocation.getLatitude(); //this line throws NullPointerException
longitude = myLocation.getLongitude();

addOnSuccessListener()中的最后一个字是“听众”。这表明您正在注册一个事件监听器。通常(尽管并非总是)这样的监听器仅在未来事件中被调用。很可能 onSuccess()稍后才会被调用。但是,您的程序继续运行,然后您尝试调用 getLatitude()myLocation 。因为我们还没有做任何事情来给 myLocation 赋值,它将是 null ,你会崩溃并显示 NullPointerException .

因此,虽然您的第一个代码 fragment 确实将值分配给 myLocation ,作为程序员,您必须假设它不会这样做,直到将来某个时候(例如,当我们获得新的位置修复时)。不要尝试使用myLocation直到onSuccess()已为其分配了一个值。

关于java - getLatitude 和 getLongitude 抛出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46023036/

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