-1

BaseAdapter显示数据时出现空指针异常CustomListview

public View getView(final int position, View convertView, ViewGroup parent){
            // TODO Auto-generated method stub
         ViewHolder holder = null;       

                 LayoutInflater layout = getActivity().getLayoutInflater();

          convertView = layout.inflate(R.layout.player, null);           

          View vi = convertView;

            if (vi == null) {

                    holder = new ViewHolder();              

                    holder.tProduct = (TextView) vi.findViewById(R.id.product);
                    holder.tTitle = (TextView) vi.findViewById(R.id.title);            

                    convertView.setTag(holder);                     

            } else {
                 holder = (ViewHolder) vi.getTag();
            }


            holder.tProduct.setText(description.get(position));
                holder.tTitle.setText(title.get(position));         

        return vi;
        }
4

3 回答 3

0

尝试这个::

LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);  
            View layout = layoutInflater.inflate(R.layout.new_popup_layout, null);  

希望能帮助到你!!

于 2013-08-22T12:12:03.523 回答
0

你必须做这样的事情

在您的适配器中创建一个myInflater像这样的变量

private LayoutInflater myInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

然后在您的代码中使用它myInflater而不是getActivity()您正在使用的。

convertView = myInflater.inflate(R.layout.player, null);

编辑

对我来说,这种方式是最有效的方式,因为您只能获得一次上下文。如果你直接把它放在getView你会在每次getView调用时访问它。

于 2013-08-22T12:15:04.777 回答
0

尝试这样的事情..

public View getView(final int position, View convertView, ViewGroup parent){
        // TODO Auto-generated method stub
     ViewHolder holder;       



      View vi = convertView;

        if (vi == null) {
                  // here mContext is you got in constructor from activity
                LayoutInflater layout = mContext.getLayoutInflater();

                vi= layout.inflate(R.layout.player,parent,false);  

                holder = new ViewHolder();              

                holder.tProduct = (TextView) vi.findViewById(R.id.product);
                holder.tTitle = (TextView) vi.findViewById(R.id.title);            

                vi.setTag(holder);                     

        } else {
             holder = (ViewHolder) vi.getTag();
        }


        holder.tProduct.setText(description.get(position));
        holder.tTitle.setText(title.get(position));         

    return vi;
    }
于 2013-08-22T12:16:44.030 回答