java - java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“double android.location.Location.getLongitude()”
问题描述
我不明白为什么应用程序关闭:
java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLongitude()' on a null object reference
private void getWeatherInformation() {
compositeDisposable.add(mSevice.getWeatherByLatLhg(String.valueOf(Common.current_location),
String.valueOf(Common.current_location.getLongitude()),
Common.APP_ID,
"metric")
.subscribeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<WeatherResponse>() {
@Override
public void accept(WeatherResponse weatherResponse) throws Exception {
Picasso.get().load(new StringBuilder("https://openweathermap.org/img/wn/")
.append(weatherResponse.getWeather().get(0).getIcon()).append(".png").toString()).into(imageWeather);
cityName.setText(weatherResponse.getName());
description.setText(new StringBuilder("Weather in ")
.append(weatherResponse.getName()).toString());
temperature.setText(new StringBuilder(String.valueOf(weatherResponse.getMain().getTemp()))
.append("°C"));
dateTime.setText(Common.convertUnixToDate(weatherResponse.getDt()));
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
Toast.makeText(getActivity(), ""+throwable.getMessage(), Toast.LENGTH_SHORT).show();
}
})
);
解决方案
Common.current_location.getLongitude() 是你的问题
Common 或 Common.current_location 为 null(未分配值),因此您正在调用 getLongitude() 方法对不存在的东西。
推荐阅读
- nginx - Nginx 443 端口已经被它自己使用了?
- reactjs - 通过 React js 使用三元运算符进行具有 >2 条件的内联样式
- c++ - 使用半字节/亚 8 位值填充类似于 IP 标头的结构
- javascript - 如何在 svelte js 中突出显示活动的导航栏链接
- c# - 分配实体实例而不是实体 ID 创建新记录
- php - Laravel 中的模型可以应用多个查询吗?
- python - 为 Python 2.7 创建可执行文件
- ruby-on-rails - 具有特定 ID 的嵌套路由的 Rails 路由别名
- python - 不显示 discord.py 的更新角色
- macos - macOS 使用 kevents 监控新的分叉进程