Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8277461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:41:52+00:00 2026-06-08T08:41:52+00:00

Im building an listview with sections. i was using this answer of a post

  • 0

Im building an listview with sections. i was using this answer of a post and asked a question before, but am stuck again. I think it is a pretty weird error.

When i start my activity, i can see the list on the screen, just as i want it. But the moment i try to start scrolling the activity crashes. I thought i implemented everything the same way, but apparently im not.
My adapter:

public class DelftAdapter extends BaseAdapter {
    
    
    private static final int TYPE_ITEM = 0;
    private static final int TYPE_SECTION = 1;
    private Activity activity;
    private  List<ListItem> listItems;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 
    private final int[] bgColors = new int[] { R.color.list_odd, R.color.list_even };

    
    public DelftAdapter(Activity a, ArrayList<ListItem> li) {
        activity = a;
        listItems = li;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());

    }

    public int getCount() {
        return listItems.size();
    }

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

    public long getItemId(int position) {
        return position;
    }
  
    @Override
    public int getItemViewType(int position) {
        return listItems.get(position).isSection() ? TYPE_SECTION : TYPE_ITEM;
    }

    @Override
    public int getViewTypeCount() {
        return 2;  // sectionheader and regular item
    }
   
    
    public View getView(int position, View convertView, ViewGroup parent) {
        int type = getItemViewType(position);
        View vi=convertView;
            final ListItem li = listItems.get(position);
            ItemViewHolder itemHolder;
            SectionViewHolder sectionHolder;
  
             switch (type) {
             case TYPE_SECTION: // is sectionheader
                 if (vi == null) { //convertview==null
                sectionHolder = new SectionViewHolder();
                vi = inflater.inflate(R.layout.sectionedlistitem, null);
                vi.setOnClickListener(null);
                vi.setOnLongClickListener(null);
                vi.setLongClickable(false);
                sectionHolder.title = (TextView) vi.findViewById(R.id.list_header_title);
               }else{//convertview is not null
                   sectionHolder = (SectionViewHolder)vi.getTag();
               }
                   SectionItem si = (SectionItem)li;
                   sectionHolder.title.setText(si.getTitle());
                break;
             case TYPE_ITEM:// no sectionheader
                if (vi == null) { //convertview==null
                    itemHolder = new ItemViewHolder();
                    vi = inflater.inflate(R.layout.singlelistitem, null);
                    itemHolder.name=(TextView)vi.findViewById(R.id.tvname);
                    itemHolder.tip=(TextView)vi.findViewById(R.id.tvtip);
                    itemHolder.image=(ImageView)vi.findViewById(R.id.image);
                }else{  // convertview != null
                    itemHolder = (ItemViewHolder)vi.getTag();
                }
                ListData ld = (ListData)li;
                itemHolder.name.setText(ld.name);
                itemHolder.tip.setText(ld.tip);
                        if (ld.photoUrl != null ){
                            imageLoader.DisplayImage(ld.photoUrl, itemHolder.image);
                        }else{
                            itemHolder.image.setImageURI(Uri.fromFile(new File("//assets/nopic.png")));
                        }
                        // alternating colors
                    int colorPos = position % bgColors.length;
                    vi.setBackgroundResource(bgColors[colorPos]);
                    break;
                 }
            
            
        return vi;
    
}
    public static class SectionViewHolder {
        public TextView title;
    }
    
    public static class ItemViewHolder {
        public TextView name;
        public TextView tip;
        public ImageView image;
    }
    
}

I build two ViewHolders for the two different kind of views. The error that occurs is NullPointerException on the itemHolder.name.setText(ld.name); line.
The thing i don’t get is that the code works for the first few entrys but fails when i start scrolling. In the data i’m using, name and tip are never empty, only photoUrl might be but that is covered in the code.

Anyone knows why this piece of code is failing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T08:41:53+00:00Added an answer on June 8, 2026 at 8:41 am

    In the code paths where you create a new viewholder and inflate a new view, you never actually store the viewHolder in the Views tag, so when you scroll and get an exisitng view, view.gettag() returns null, and later when you try and use the ViewHolder you get the Null Pointer Exception. You need to add the calls to setTag().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am building an app using listview and lazy adapter based on @fedor post
I'm building an ASP.NET application. I'm using a ListView to show some Entities however
I am building a WPF application in C# using VS2010 I have a listview
building a site using PHP and MySQL that needs to store a lot of
Im building a contact application. In the main activity i have a listview, where
Building an app using android.support.v4.app , targeting API level 8. Everything's working as planned
experts, I know I cannot put a ListView inside a ScrollView, but the UI
I am building an application that has the functionality like 3 tabs created using
I am building this app and I can't make it work. Here is the
I've just started out building a very simple score screen for my app but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.