Sugar ORM不会将数据保存到数据库

时间:2018-10-15 19:23:09

标签: java android retrofit retrofit2 sugarorm

请给我解决我的麻烦。

这是我的根模型类,实际上是自动生成的POJO:

public static void getWeatherForecastOneDay(final TextView conditionTextView,
                                            final TextView temperatureTextView,
                                            final TextView temperatureFeelsLikeTextView,
                                            final TextView pressureTextView,
                                            final TextView windSpeedTextView,
                                            final TextView windDirectionTextView,
                                            final TextView humidityTextView,
                                            final ImageView weatherPicture) {
    context = MainActivity.context;
    String KEY = context.getResources().getString(R.string.api_key);


    Presenter.getWeatherForecastApi().getForecast7Days(KEY, "Nizhny Novgorod", 7, "ru")
            .enqueue(new Callback<WeatherRootObject>() {
                @Override
                public void onResponse(Call<WeatherRootObject> call, Response<WeatherRootObject> response) {


                    WeatherRootObject weatherRootObject = new WeatherRootObject(
                            response.body().getCurrent(),
                            response.body().getLocation(),
                            response.body().getForecast());
                    weatherRootObject.save();

                    WeatherRootObject weatherRootObjectSaved = WeatherRootObject.findById(WeatherRootObject.class, 1);


                    humidity = weatherRootObjectSaved.getCurrent().getHumidity();
                    imageURI = "http:" + weatherRootObjectSaved.getCurrent().getCondition().getIcon();
                    condition = weatherRootObjectSaved.getCurrent().getCondition().getText();
                    windSpeed = weatherRootObjectSaved.getCurrent().getWindKph();
                    windDirection = weatherRootObjectSaved.getCurrent().getWindDir();
                    pressure = weatherRootObjectSaved.getCurrent().getPressureMb();
                    temperature = weatherRootObjectSaved.getCurrent().getTempC();
                    temperatureFeelsLike = weatherRootObjectSaved.getCurrent().getFeelslikeC();



                    humidityTextView.setText(String.valueOf(humidity));
                    Picasso.get().load(imageURI).into(weatherPicture);
                    conditionTextView.setText(String.valueOf(condition));
                    windSpeedTextView.setText(String.valueOf(windSpeed));
                    windDirectionTextView.setText(String.valueOf(windDirection));
                    pressureTextView.setText(String.valueOf(pressure));
                    temperatureTextView.setText(String.valueOf(temperature));
                    temperatureFeelsLikeTextView.setText(String.valueOf(temperatureFeelsLike));



                }

                @Override
                public void onFailure(Call<WeatherRootObject> call, Throwable t) {
                    Log.i("GETTA", t.toString());


                }
            });


}

我试图将Object保存到数据库中只是为了对其进行缓存并将其显示在屏幕上。这是我在Presenter中的方法,该方法用于下载数据并将其显示在屏幕上:

select 
    timestamp, 
    group_concat(case when dev="01" then event else "" end, "") as D01, 
    group_concat(case when dev="02" then event else "" end, "") as D02  
from 
    table01 
group by 
    timestamp;

因此,当我只是使用翻新版下载信息时,它就可以完美运行。当我尝试保存数据并在填充TextViews时使用它时,我总是会收到NullPointerException:     尝试在空对象引用上调用虚拟方法'int com.weather.Model.Current.getHumidity()'

因此,看来,当我请求数据时数据并没有存储在DB中,我也无法理解为什么。

0 个答案:

没有答案