空指针异常在列表视图,从而为listItems中分离的布局? [英] NullPointer Exception in listview,creating separate layout for listitems?

查看:205
本文介绍了空指针异常在列表视图,从而为listItems中分离的布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只当我向下滚动创建一个包含两个类型的项目文本或image.But我得到空指针异常的列表视图。
我的适配器的code是这样的。

 公共类ListViewAdapter延伸BaseAdapter {ArrayList的<&的ListModel GT; myList中=新的ArrayList<&的ListModel GT;();
LayoutInflater吹气;
上下文语境;
私有静态最终诠释TYPE_ITEM1 = 1;
私有静态最终诠释TYPE_ITEM2 = 2;
公共ListViewAdapter(上下文的背景下,ArrayList的<&的ListModel GT; myList中){
    this.myList = myList中;
    this.context =背景;
    吹气= LayoutInflater.from(this.context);}
int型的;@覆盖
公众诠释getItemViewType(INT位置){
    ListModel的ListModel的= myList.get(位置);
    字符串数据= listModel.getType();
  / *如果(data.equals(文本)){
        返回类型1;
    }否则如果(data.equals(图像)){
        返回2型;    }返回0; * /    如果(data.equals(文本)){
        键入= TYPE_ITEM1;
    }否则如果(data.equals(图像)){
        键入= TYPE_ITEM2;
    }
    返回类型;}@覆盖
公众诠释getViewTypeCount(){
    返回myList.size()+ 1;
}
@覆盖
公众诠释的getCount(){
    返回myList.size();
}
@覆盖
公众的ListModel的getItem(INT位置){
    //返回myList.get(位置);    如果(位置> = myList.size()){
        返回null;
    }
    返回myList.get(位置);
}@覆盖
众长getItemId(INT位置){
    返回的位置;
}
@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
    ViewHolder支架=无效;
    视图V = convertView;
    TextView中的TextView = NULL;
    ImageView的ImageView的= NULL;
    整型= getItemViewType(位置);
    的System.out.println(getView+位置++ V +TYPE =+型);
    如果(V == NULL){
        持有人=新ViewHolder();
        如果(类型== TYPE_ITEM1){
            V = inflater.inflate(R.layout.list_text,NULL);
            的TextView =(TextView中)v.findViewById(R.id.text);
        }否则如果(类型== 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(保持器);
    }其他{
        支架=(ViewHolder)v.getTag();
    }
    ListModel的模型= myList.get(位置);
    字符串数据类型= model.getType();    如果(datatype.equals(文本)){        holder.textView.setText(model.getData());    }否则如果(datatype.equals(图像)){        UrlImageViewHelper.setUrlDrawable(holder.imageView,model.getData());
    }    返回伏;
}公共静态类ViewHolder {
    公众的TextView的TextView;
    公共ImageView的ImageView的;
}}

该错误是
显示java.lang.NullPointerException 在第117行,这是TextView的,


解决方案

这是一个典型的 ArrayIndexOutOfBoundsException异常。不要忘记,数组在Java中(如在大多数的编程语言)的0索引,meanign第一个元素是指数 0 (不是 1 )。

您还没有表现出任何调用code,但最有可能你的一些code被调用方法的getItem() getView()与值的位置 2 其中,它应该是 1 (访问到第二元件中的列表)。

如果您无法控制调用者(也许是从第三方库),那么我建议:


  • 抵消-1 位置的价值。但是,这可能会破坏你的code的其他部分。

  • 在code添加一些保护prevent例外,如:


 公开的ListModel的getItem(INT位置){
    如果(位置> = myList.size()){
        // TODO:记录错误,显示消息等。
        返回null; //或默认值
    }
    返回myList.get(位置);
}


这也可能是您选择了 TYPE_ITEM1 TYPE_ITEM2 没有得到很好的,如果他们必须重新$值p $数组中psent指数。考虑使用 0 1 代替。

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,

解决方案

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.

这篇关于空指针异常在列表视图,从而为listItems中分离的布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆