【问题标题】:java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLongitude()' on a null object reference [duplicate]java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“double android.location.Location.getLongitude()”[重复]
【发布时间】:2019-11-21 07:34:20
【问题描述】:

我不明白应用为什么会关闭:

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();
                    }
                })

        );

【问题讨论】:

    标签: java android nullpointerexception android-location


    【解决方案1】:

    Common.current_location.getLongitude() 是你的问题

    Common 或 Common.current_location 都为 null(未分配值),因此您正在对不存在的东西调用 getLongitude() 方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-22
      • 2016-07-30
      • 2015-02-09
      • 2020-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-06
      相关资源
      最近更新 更多