Android错误-无效索引0,大小为0

时间:2018-12-27 05:47:23

标签: java android object indexing null

我上面的标题是面对问题。

以前,由于没有初始化列表对象,我有错误“在空对象引用上的java.lang.Object java.until.List.get(int)”。但是在初始化之后,我现在遇到了index = 0,null = 0错误的问题。我只是停留在如何解决该错误上……

WeatherResult.java -在这个课程中,我只显示列表编码

 private List<Weather>weathers= new ArrayList<Weather>(); //I get the 'java.until.List.get(int)on a null' error when I didn't initialize a Arraylist for this
public List<Weather> getWeathers() {
        return weathers;
    }

    public void setWeathers(List<Weather> weathers) {
        this.weathers = weathers;
    }

Weather.java

public class Weather {
    private int id ;
    private String main ;
    private String description ;
    private String icon ;

    public Weather(){

    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getMain() {
        return main;
    }

    public void setMain(String main) {
        this.main = main;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

}     

main.java

    public class TodayWeatherFragment extends Fragment {
        ImageView img_weather;
        TextView txt_city_name,txt_humidity,txt_sunrise,txt_sunset,txt_pressure,txt_temperature,txt_description,txt_date_time,txt_wind,txt_geocoord;
        LinearLayout weather_panel;
        ProgressBar loading;
        CompositeDisposable compositeDisposable;
        IOpenWeatherMap mService;

        static TodayWeatherFragment instance;
        public static TodayWeatherFragment getInstance(){
            if(instance==null){
                instance=new TodayWeatherFragment();
            }
            return instance;
        }

        public TodayWeatherFragment() {
            // Required empty public constructor
            compositeDisposable= new CompositeDisposable();
            Retrofit retrofit= RetrofitClient.getInstance();
            mService= retrofit.create(IOpenWeatherMap.class);
        }


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View itemView= inflater.inflate(R.layout.fragment_today_weather, container, false);
            img_weather=(ImageView)itemView.findViewById(R.id.img_weather);
            txt_city_name=(TextView)itemView.findViewById(R.id.text_city_name);
            txt_date_time=(TextView)itemView.findViewById(R.id.text_date_time);
            txt_description=(TextView)itemView.findViewById(R.id.text_description);
            txt_geocoord=(TextView)itemView.findViewById(R.id.txt_geocoord);
            txt_humidity=(TextView)itemView.findViewById(R.id.txt_humidity);
            txt_pressure=(TextView)itemView.findViewById(R.id.txt_pressure);
            txt_sunrise=(TextView)itemView.findViewById(R.id.txt_Sunrise);
            txt_sunset=(TextView)itemView.findViewById(R.id.txt_sunset);
            txt_temperature=(TextView)itemView.findViewById(R.id.txt_temperature);
            txt_wind=(TextView)itemView.findViewById(R.id.txt_wind);

            weather_panel=(LinearLayout)itemView.findViewById(R.id.weather_panel);
            loading=(ProgressBar)itemView.findViewById(R.id.loading);

            getWeatherInformation();
            return itemView;
        }

        private void getWeatherInformation() {
            compositeDisposable.add(mService.getWeatherByLatLng(String.valueOf(Common.current_location.getLatitude()),
                    String.valueOf(Common.current_location.getLongitude()),
                    Common.APP_ID,"metric").subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<WeatherResult>() {
                        @Override
                        public void accept(WeatherResult weatherResult) throws Exception {


                                Picasso.get().load(new StringBuilder("https://openweathermap.org/img/w/")
                                        .append(weatherResult.getWeathers().get(0).getIcon())
                                        .append(".png").toString()).into(img_weather);


                            txt_city_name.setText(weatherResult.getName());
                            txt_description.setText(new StringBuilder("Weather in").append(weatherResult.getName().toString()));
                            txt_temperature.setText(new StringBuilder(String.valueOf(weatherResult.getMain().getTemp())).append("°C").toString());
                            txt_date_time.setText(Common.convertUnixToDate(weatherResult.getDt()));
                            txt_pressure.setText(new StringBuilder(String.valueOf(weatherResult.getMain().getPressure())).append(" hpa").toString());
                            txt_humidity.setText(new StringBuilder(String.valueOf(weatherResult.getMain().getHumidity())).append(" %").toString());
                        txt_sunrise.setText(Common.convertUnixToHour(weatherResult.getSys().getSunrise()));
                        txt_sunset.setText(Common.convertUnixToHour(weatherResult.getSys().getSunset()));
                        txt_geocoord.setText(new StringBuilder(weatherResult.getCoord().toString()).toString());

                        weather_panel.setVisibility(View.VISIBLE);
                        loading.setVisibility(View.GONE);

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        Toast.makeText(getActivity(),""+throwable.getMessage(),Toast.LENGTH_SHORT).show();

                    }
                })
        );
    }
    }

1 个答案:

答案 0 :(得分:-1)

确保WeatherResult对象不为null。 并确保WeatherResult的List对象不为null,并且列表大小必须> 0