自定义列表视图抛出空指针异常

时间:2016-10-03 19:56:51

标签: android listview arraylist

我在android中创建自定义ListView。面临的问题是我的适配器代码。即使其他所有内容都按顺序显示,它也会抛出NullPointerException。示例代码如下:

public class CustomAdapter extends BaseAdapter {

   private Context ctx;
   private List<LearnLangs> langsList;
   private LayoutInflater layoutInflater;

   public CustomAdapter(Context ctx, List<LearnLangs> langsList) {
       this.ctx = ctx;
       this.langsList = langsList;
   }

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

   @Override
   public Object getItem(int position) {
       return position;
   }

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

   public static class ViewHolder{

       public ImageView img;
       public TextView title;
       public TextView sub;
   }

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

       ViewHolder holder;
       LearnLangs langs = langsList.get(position);

       if (convertView == null) {
           layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           layoutInflater.inflate(R.layout.custom_list, parent, false);

           holder = new ViewHolder();
           //error occurs on this line below
           holder.img = (ImageView) convertView.findViewById(R.id.list_img);
           holder.title = (TextView) convertView.findViewById(R.id.list_title);
           holder.sub = (TextView) convertView.findViewById(R.id.list_subT);

           convertView.setTag(holder);
       }

       else
       {
           holder = (ViewHolder) convertView.getTag();

           holder.img.setImageResource(langs.getImgId());
           holder.title.setText(langs.getNameLang());
           holder.sub.setText(langs.getnTuts());
       }
       return convertView;
   }
}

抛出异常:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
                                                                           at com.example.joey.sunshine.CustomAdapter.getView(CustomAdapter.java:62)
                                                                           at android.widget.AbsListView.obtainView(AbsListView.java:2347)
                                                                           at android.widget.ListView.makeAndAddView(ListView.java:1864)
                                                                           at android.widget.ListView.fillDown(ListView.java:698)
                                                                           at android.widget.ListView.fillFromTop(ListView.java:759)
                                                                           at android.widget.ListView.layoutChildren(ListView.java:1673)
                                                                           at android.widget.AbsListView.onLayout(AbsListView.java:2151)
                                                                           at android.view.View.layout(View.java:15761)

什么似乎是问题?

3 个答案:

答案 0 :(得分:0)

您正在获得异常,因为您根本没有使用正在膨胀的视图。按以下方式更改getview()方法的代码:

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

    View mView = convertView;
    ViewHolder holder;
    LearnLangs langs = langsList.get(position);

    if (mView == null) {
        layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = layoutInflater.inflate(R.layout.custom_list, parent, false);

        holder = new ViewHolder();
        //error occurs on this line below
        holder.img = (ImageView) mView.findViewById(R.id.list_img);
        holder.title = (TextView) mView.findViewById(R.id.list_title);
        holder.sub = (TextView) mView.findViewById(R.id.list_subT);

        mView.setTag(holder);
    }

    else
    {
        holder = (ViewHolder) mView.getTag();

        holder.img.setImageResource(langs.getImgId());
        holder.title.setText(langs.getNameLang());
        holder.sub.setText(langs.getnTuts());
    }
    return mView;
}

答案 1 :(得分:0)

只需更改此行:

layoutInflater.inflate(R.layout.custom_list, parent, false); 

到此:

convertView = layoutInflater.inflate(R.layout.custom_list, parent, false); 

修改

public AdapterConstructor() {
    layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); //move this line inside the constructor
}

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

    ViewHolder holder;
    LearnLangs langs = langsList.get(position);

    if (convertView == null) {
        convertView = layoutInflater.inflate(R.layout.custom_list, parent, false);

        holder = new ViewHolder();
        //error occurs on this line below
        holder.img = (ImageView) convertView.findViewById(R.id.list_img);
        holder.title = (TextView) convertView.findViewById(R.id.list_title);
        holder.sub = (TextView) convertView.findViewById(R.id.list_subT);

        convertView.setTag(holder);
    }

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

        holder.img.setImageResource(langs.getImgId());
        holder.title.setText(langs.getNameLang());
        holder.sub.setText(langs.getnTuts());
    return convertView;
}

答案 2 :(得分:0)

只需按以下方式更改getView()方法:

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

   ViewHolder holder;
   LearnLangs langs = langsList.get(position);

   if (convertView == null) {
       layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       convertView = layoutInflater.inflate(R.layout.custom_list, parent, false);

       holder = new ViewHolder();
       //error occurs on this line below
       holder.img = (ImageView) convertView.findViewById(R.id.list_img);
       holder.title = (TextView) convertView.findViewById(R.id.list_title);
       holder.sub = (TextView) convertView.findViewById(R.id.list_subT);

       convertView.setTag(holder);
   }

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

   holder.img.setImageResource(langs.getImgId());
   holder.title.setText(langs.getNameLang());
   holder.sub.setText(langs.getnTuts());

   return convertView;
}