- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
请您解释一下为什么 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/
Android 的 location.getLatitude() 方法是否提供大地纬度或地心纬度? 如果是大地测量,它是基于 WGS84 还是 GR80 椭球体? API 规范令人惊讶地缺乏,它只声明
我是编程新手,正在学习 Google Location API 教程。我已正确执行所有步骤,但是,以下代码在 getlatitude() 和 getlongitude() 处给我错误。错误:无法解析方
我正在编写跟踪 map 。在我想要分配的 getDeviceLocation 方法中: prevLat=mLastKnownLocation.getLatitude(); prevLng=mLastK
我收到以下运行时错误:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“double android.location.Location.getLatitu
我正在开发一个应用程序,只要您单击获取位置 (gpsBtn),GPS 就会启用,用户的位置将显示在 TextView 中。我收到 java.lang.NullPointerException 错误,应
我正在处理的 Android 应用程序有问题。该应用程序工作正常,直到我添加了两行代码来获取用户的纬度和经度。当我添加此代码时,我的应用程序无缘无故崩溃。这是带有 getLatitude 和 getL
这个问题在这里已经有了答案: Android getlastknownlocation returns null (1 个回答) 关闭 9 年前。 我在这一行收到一个空指针异常: double la
这个问题已经有答案了: What is a NullPointerException, and how do I fix it? (12 个回答) 已关闭 4 年前。 请您解释一下为什么 getLon
这个问题在这里已经有了答案: What is a NullPointerException, and how do I fix it? (12 个答案) 关闭 8 年前。 我正在尝试获取当前的纬度和
我真的遇到了这个错误,已经尝试了我能想到的一切来避免 getLatitude 方法上的 NPE... 有人可以检查我的代码并推荐解决方案吗? 只有当我在纬度和经度的某些坐标中进行硬编码时,它才有效,但
我有一种方法可以在 map 上显示设备的当前位置。我使用 FusedLocationProviderClient 因为它似乎是最准确的。 在 currentLocation.getLatitude()
如何将 getLongitude() 和 getLatitude() 的值转换为 EditText 可以接受的格式? 如果我像这样把它们放到 Toast 中就可以了 String message =
状态:大约一周后接受最佳可用答案并表示感谢。等待/邀请更多答案并引用 这件事很复杂。我真的很感谢所有评论员,特别是 asciimo、Gabe Sechan、AlexWien。请在适当研究后进一步投票和
为什么我不知道,我犯了这个错误。程序之前运行过。但现在我犯了这个错误。谢谢你的帮助... MenuActivity.java: final Context context = this; privat
所以我整天都在为此工作,我快要失去理智了。我正在尝试从谷歌的位置 api 获取坐标,以便从另一个 api (forcast.io) 获取天气。我的代码已经达到这样的程度,即 google api 正在
我一直在寻找答案,但也许我只是没有使用正确的术语。我正在创建一个应用程序,它将访问数据库以返回距用户位置一定距离内的其他用户的列表。我从未使用过此类数据,而且我真的不知道这些值的含义。我想使用 MyS
我正在尝试使用 openstreetmap osmosis 读取机场的 pbf 文件并提取登机口和跑道等特征。 我使用的代码类似于:http://www.javaoptimum.com/how-to-
我想要做的就是让用户获得他喜欢的类型的位置列表。例如,如果输入是医院,我的应用程序将使用搜索字符串“医院”打开谷歌地图。但正如文档中所建议的那样,使用像 geo:0,0?q=hospital 这样的地
这个问题已经有答案了: What is a NullPointerException, and how do I fix it? (12 个回答) 已关闭 5 年前。 我正在 android 中创建一
当我使用 Mapbox 时,我遇到了与 GPS 相关的问题。 public class MainActivity extends AppCompatActivity implements OnMapR
我是一名优秀的程序员,十分优秀!