Search code examples
androidadapterindexoutofboundsexception

java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 only once


I got the following exception only once at getItem of adapter.Maybe it was a device or connection problem but my app crashed.How can i make sure that my app doen't crashes with this exception if there is problem in device?

 java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0

adapter

   @Override
        public Object getItem(int position) {
            return items.get(position);
        }

Solution

  •   @Override
      public Object getItem(int position) {
          if(items.size() == 0) {
                return null;
          }
          return items.get(position);
      }