来自适配器setOnClickListener的空指针引用

时间:2016-06-27 03:22:45

标签: android listview nullpointerexception

我有以下代码,我得到一个空指针异常(见下文)。我想检索视图持有者指向的userName。我甚至试过通过view(v)本身检索它,但我得到了同样的例外。我不明白为什么值为null。

    public class ContactAdapter extends BaseAdapter {

        private Context mContext;


        public ContactAdapter(Context context) {

            mContext = context;
        }


        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder holder;

            if (convertView == null) {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.item_contact, null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);

            } else {

                holder = (ViewHolder) convertView.getTag();
            }

    holder.delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    try {
                        ViewHolder vv = (ViewHolder)v.getTag();
                        TextView tv = (TextView) vv.userName.getText(); // Exception here - Line 120
                    }catch(Exception xx){

                    }
                }
     });
}

    private class ViewHolder {

            TextView userName;
            ImageView add, delete, block,profImage;

            public ViewHolder(View view) {

                userName = (TextView) view.findViewById(R.id.user_name);
                add = (ImageView) view.findViewById(R.id.add);
                delete = (ImageView) view.findViewById(R.id.delete);
                block = (ImageView) view.findViewById(R.id.block);
                profImage = (ImageView) view.findViewById(R.id.profile_image);
            }
        }

以下是例外情况: enter image description here

4 个答案:

答案 0 :(得分:0)

view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    ViewHolder vv = (ViewHolder)v.getTag();
                    TextView tv = (TextView) vv.userName.getText(); 
                }catch(Exception xx){

                }
            }
 });

你有没有在viewholder构造函数中尝试过这个...我认为你得到了空指针异常,因为你在holder.delete上设置了click listner

答案 1 :(得分:0)

替换' ViewHolder持有人;'最终的ViewHolder持有者;'并且在onClick方法中,只需通过执行“holder.userName.getText()'访问视图”。等...

答案 2 :(得分:0)

试试这个,

ViewHolder vv = (ViewHolder)convertView.getTag();

答案 3 :(得分:0)

holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    ViewHolder vv = (ViewHolder)v.getTag();
                    TextView tv = (TextView) vv.userName.getText(); // Exception here - Line 120
                }catch(Exception xx){

                }
            }
 });

您为convertView设置了标记,但是您从convertView的子设置getTag()。所以它将是null

修复它:

  1. 将ViewHolder更改为最终

    最终ViewHolder持有者;

  2. 直接从持有人处获取文字

    holder.delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    try {
                        TextView tv = (TextView) holder.userName.getText();
                    }catch(Exception xx){
    
                    }
                }
     });