NullPointer Exception in listview,creating separate layout for listitems?

与世无争的帅哥 提交于 2019-12-14 03:28:17

问题


I created a listview that contains two types of items either text or image.But i am getting nullpointer exception only on when i scrolling down. my adapter's code is this.

public class ListViewAdapter extends BaseAdapter {

ArrayList<ListModel> myList = new ArrayList<ListModel>();
LayoutInflater inflater;
Context context;
private static final int TYPE_ITEM1 = 1;
private static final int TYPE_ITEM2 = 2;


public ListViewAdapter(Context context, ArrayList<ListModel> myList) {
    this.myList = myList;
    this.context = context;
    inflater = LayoutInflater.from(this.context);

}


int type;

@Override
public int getItemViewType(int position) {
    ListModel listModel = myList.get(position);
    String data = listModel.getType();
  /*  if (data.equals("Text")) {
        return type1;
    } else if (data.equals("Image")) {
        return type2;

    }return 0;*/

    if (data.equals("Text")) {
        type = TYPE_ITEM1;
    } else if (data.equals("Image")) {
        type = TYPE_ITEM2;
    }
    return type;

}

@Override
public int getViewTypeCount() {
    return myList.size() + 1;
}


@Override
public int getCount() {
    return myList.size();
}


@Override
public ListModel getItem(int position) {
    // return myList.get(position);

    if (position >= myList.size()) {
        return null;
    }
    return myList.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    View v = convertView;
    TextView textView = null;
    ImageView imageView = null;
    int type = getItemViewType(position);
    System.out.println("getView " + position + " " + v + " type = " + type);
    if (v == null) {
        holder = new ViewHolder();
        if (type == TYPE_ITEM1) {
            v = inflater.inflate(R.layout.list_text, null);
            textView = (TextView) v.findViewById(R.id.text);
        } else if (type == TYPE_ITEM2) {
            v = inflater.inflate(R.layout.list_image, null);
            imageView = (ImageView) v.findViewById(R.id.imgView);
        }
        holder.textView = textView;
        holder.imageView = imageView;
        v.setTag(holder);
    } else {
        holder = (ViewHolder) v.getTag();
    }
    ListModel model = myList.get(position);
    String datatype = model.getType();

    if (datatype.equals("Text")) {

        holder.textView.setText(model.getData());

    } else if (datatype.equals("Image")) {

        UrlImageViewHelper.setUrlDrawable(holder.imageView, model.getData());


    }

    return v;
}

public static class ViewHolder {
    public TextView textView;
    public ImageView imageView;


}}

The error is java.lang.NullPointerException in line 117, it is the textView,


回答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.
        return null; // 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.




回答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



来源:https://stackoverflow.com/questions/31336847/nullpointer-exception-in-listview-creating-separate-layout-for-listitems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!