Skip to content Skip to sidebar Skip to footer

Java.lang.IllegalArgumentException: Can't Have A ViewTypeCount < 1

I'm getting this error: java.lang.IllegalArgumentException: Can't have a viewTypeCount < 1 I'm pretty sure I know exactly what's causing it, but I don't know how to fix it. My

Solution 1:

I think you miss the point of ViewTypeCount. You should return the number of Different View Types in your list. This is important for recycling of the Views inside the List.

Imaging you have 2 Types of Listitems, one with a white Background and one with black Background. When you return 2 as ViewTypeCount the Listview knows ok, there a 2 types of Listitems and will not mix them up in the getView view recycling.

so just use:

   public int getViewTypeCount() {                 
        return 1;
    }

or dont override that method at all.


Solution 2:

Use this

@Override
public int getViewTypeCount() {
    if(getCount() > 0){
        return getCount();
    }else{
        return super.getViewTypeCount();
    }

}


Solution 3:

getViewTypeCount returns the number of different types of views this adaptor can return. Since you're only returning one type of view, this should just return 1.


Solution 4:

Change this function in your Adapter class:

@Override
public int getViewTypeCount() {
   return getCount();
}

to:

public int getViewTypeCount() {
   if(getCount()<1) return 1;
   return getCount();
}

**note: avoid @Override


Post a Comment for "Java.lang.IllegalArgumentException: Can't Have A ViewTypeCount < 1"