在列表视图适配器中使用视图持有者时出现空指针异常

时间:2014-11-25 01:10:32

标签: android android-listview nullpointerexception

我尝试在我的getView()方法中实现视图持有者,然而,它崩溃并显示视图中的所有视图持有者为空,为什么,这是我的代码。

        if(view==null){
            viewHolder=new ViewHolder();
            view=inflater.inflate(R.layout.find_new_thing_item,null);
            viewHolder.progressBar=(ProgressBar)view.findViewById(R.id.find_new_thing_item_progress_bar);
            viewHolder.mainPictureImageView=(ImageView)view.findViewById(R.id.find_new_thing_item_main_image);
            viewHolder.userIconImageView=(ImageView)view.findViewById(R.id.find_new_thing_item_user_icon);
            viewHolder.thingsNameTextView=(TextView)view.findViewById(R.id.find_new_thing_item_name);
            viewHolder.priceTextView=(TextView)view.findViewById(R.id.find_new_thing_item_price);
            viewHolder.userNameTextView=(TextView)view.findViewById(R.id.find_new_thing_item_user_name);
            viewHolder.userCommentTextView=(TextView)view.findViewById(R.id.find_new_thing_item_user_comment);
            viewHolder. numberOfLikesTextView=(TextView)view.findViewById(R.id.find_new_thing_item_number_of_likes);
            viewHolder.numberOfAddToDouListTextView=(TextView)view.findViewById(R.id.find_new_thing_item_number_of_add_to_dou_list);
            viewHolder.numberOfReplyTextView=(TextView)view.findViewById(R.id.find_new_thing_item_number_of_reply);
            view.setTag(viewHolder);
        }else {
            viewHolder=(ViewHolder)view.getTag();
        }


        new DownloadItemImage().execute(thingsItemArrayList.get(i-1).getMainPicture());
        new DownloadUserIcon().execute(thingsItemArrayList.get(i-1).getUserIcon());
        viewHolder.thingsNameTextView.setText(thingsItemArrayList.get(i - 1).getThingsName());
        viewHolder.priceTextView.setText(thingsItemArrayList.get(i-1).getPrice());
        viewHolder.userNameTextView.setText(thingsItemArrayList.get(i-1).getUserName());
        viewHolder.userCommentTextView.setText(thingsItemArrayList.get(i-1).getUserComment());
        viewHolder.numberOfLikesTextView.setText(thingsItemArrayList.get(i-1).getNumberOfLikes());
        viewHolder.numberOfAddToDouListTextView.setText(thingsItemArrayList.get(i-1).getNumberOfAddToDouList());
        if(thingsItemArrayList.get(i-1).getNumberOfReply().indexOf("评论")>=0){
            viewHolder.numberOfReplyTextView.setText(thingsItemArrayList.get(i-1).getNumberOfReply());
        }else{
            viewHolder.numberOfReplyTextView.setText("评论 0");
        }

        return view;

4 个答案:

答案 0 :(得分:3)

inflater.inflater()中,您没有指定父视图

这样做:

if(view==null){
    view=inflater.inflate(R.layout.find_new_thing_item,parent,false);
    viewHolder=new ViewHolder(view);
    view.setTag(viewHolder);
}else {
    viewHolder=(ViewHolder)view.getTag();
}
//Set values here.
//viewHolder.thingsNameTextView.setText(....);
//blah blah blah...
return view;

在viewHolder构造函数中:

public viewHolder(View view){
     viewHolder.progressBar=(ProgressBar)view.findViewById(R.id.find_new_thing_item_progress_bar);
        viewHolder.mainPictureImageView=(ImageView)view.findViewById(R.id.find_new_thing_item_main_image);
        viewHolder.userIconImageView=(ImageView)view.findViewById(R.id.find_new_thing_item_user_icon);
        viewHolder.thingsNameTextView=(TextView)view.findViewById(R.id.find_new_thing_item_name);
        viewHolder.priceTextView=(TextView)view.findViewById(R.id.find_new_thing_item_price);
        viewHolder.userNameTextView=(TextView)view.findViewById(R.id.find_new_thing_item_user_name);
        viewHolder.userCommentTextView=(TextView)view.findViewById(R.id.find_new_thing_item_user_comment);
        viewHolder. numberOfLikesTextView=(TextView)view.findViewById(R.id.find_new_thing_item_number_of_likes);
        viewHolder.numberOfAddToDouListTextView=(TextView)view.findViewById(R.id.find_new_thing_item_number_of_add_to_dou_list);
        viewHolder.numberOfReplyTextView=(TextView)view.findViewById(R.id.find_new_thing_item_number_of_reply);
}

答案 1 :(得分:0)

viewHolder=new ViewHolder();
if(view == null){
...
}

这样做。 如果你这样做,当view!= null时,viewHolder显然等于null。

答案 2 :(得分:0)

以这种方式试试......

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

             View view= convertView;
             ViewHolder viewHolder;

             if(convertView==null){

            view=inflater.inflate(R.layout.find_new_thing_item,null);

            viewHolder=new ViewHolder();

            viewHolder.progressBar=(ProgressBar)view.findViewById(R.id.find_new_thing_item_progress_bar);
            viewHolder.mainPictureImageView=(ImageView)view.findViewById(R.id.find_new_thing_item_main_image);
            viewHolder.userIconImageView=(ImageView)view.findViewById(R.id.find_new_thing_item_user_icon);
            viewHolder.thingsNameTextView=(TextView)view.findViewById(R.id.find_new_thing_item_name);
            viewHolder.priceTextView=(TextView)view.findViewById(R.id.find_new_thing_item_price);
            viewHolder.userNameTextView=(TextView)view.findViewById(R.id.find_new_thing_item_user_name);
            viewHolder.userCommentTextView=(TextView)view.findViewById(R.id.find_new_thing_item_user_comment);
            viewHolder. numberOfLikesTextView=(TextView)view.findViewById(R.id.find_new_thing_item_number_of_likes);
            viewHolder.numberOfAddToDouListTextView=(TextView)view.findViewById(R.id.find_new_thing_item_number_of_add_to_dou_list);
            viewHolder.numberOfReplyTextView=(TextView)view.findViewById(R.id.find_new_thing_item_number_of_reply);
            view.setTag(viewHolder);
        }else {
            viewHolder=(ViewHolder)view.getTag();
        }

         ........
         ..........

return view;
}

答案 3 :(得分:0)

空指针在列表视图适配器

中使用视图持有者时出现异常

如果您正在使用ButterKnife绑定视图并在持有者尝试访问视图时获取Null Pointer Exception。

因此,在添加ButterKnife的依赖/库时,可能有可能在build.gradle(app)文件中出错。

请再次检查您是否正确添加了Butterknife libaray及其annotateProcessor。

由于 我希望这个解决方案可以帮到你。