java - 如何手动设置位置中的纬度和经度?
问题描述
我试图在应用程序中手动设置纬度和经度但是,运行后给我一个错误
代码
public class Common {
public static Location NewYork;
}
配置
public void setLocation(){
Common.NewYork.setLatitude(31.963158); //error in this Line <--
Common.NewYork.setLongitude(35.930359);
fragmentAdapter();
}
错误
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.location.Location.setLatitude(double)' on a null object reference
解决方案
您从未初始化NewYork
变量,因此在setLocation()
调用时将其设置为 null。您可以通过以下方式更改:
public class Common {
// Create new Location with an empty provider name, it's not necessary here
public static Location NewYork = new Location("");
}
或者
public void setLocation() {
Common.NewYork = new Location("");
...
}
推荐阅读
- python - 如何将字符串 dict 转换为 pyspark 数据框?
- python - 如何用python写这个方程?
- c++ - 如何在 C++ 中将每个打印行增加到 2 的 n 和 n-1 的幂?
- swift - 将 UIView 的约束添加到 iPhone 的 UIView 底部有缺口
- google-chrome-devtools - Chrome 开发工具 VS Shopify 格式化文件
- kubernetes - Magnolia 从作者进程中发布 DAM 内容,杀死 pod
- python - Python:你如何在字典列表的字典列表中删除一个字典?
- python - 为什么在对 pydatatable 中的多个列应用分组后会创建重复列?
- authorization - 如何通过api网关将标头中的cognito身份验证令牌传递给lambda函数
- c# - 使用 mbedtls 生成的 RSA 签名,无法使用 C# (bouncycastle) 应用程序进行验证