Nullpointer Exception In Listview,creating Separate Layout For Listitems?
Solution 1:
It is a classic ArrayIndexOutOfBoundsException
. Don't forget that arrays in java (like in most programming language) are 0-indexed, meanign the first element is at index 0
(not 1
).
You haven't shown any caller code, but most likely some of your code are calling methods getItem()
or getView()
with a position which value is 2
where it should be 1
(to access to second element in the list).
If you cannot control the caller (maybe it is from an third-party library), then I suggest that:
- offset the value of
position
by -1. But this might break some other part of your code. - add some protection in your code to prevent exception, such as:
public ListModel getItem(int position) {
if (position >= myList.size()) {
// TODO: log an error, show a message, etc.returnnull; // or a default value
}
return myList.get(position);
}
It is also possible that the values you chose for TYPE_ITEM1
and TYPE_ITEM2
are not well if they must represent indices in arrays. Consider using 0
and 1
instead.
Solution 2:
It's probably your getItemViewType
. You should return a zero-based index (i.e. 0 or 1 not 1 or 0). From the docs:
Note: Integers must be in the range 0 to getViewTypeCount() - 1
Post a Comment for "Nullpointer Exception In Listview,creating Separate Layout For Listitems?"