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 8184793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:37:38+00:00 2026-06-07T01:37:38+00:00

This is my situation. What i have: an activity with a checkbox(called checkAll) at

  • 0

This is my situation.
What i have: an activity with a checkbox(called “checkAll”) at top-right and below a list of custom element: a TextView and a checkbox.
What i want: when clik on checkAll, then all checkbox of each item in the list are checked.
Problem: All works fine only if all items of the list are visible on the screen. When i have a list with a lot of elements (for example 20), then when click on checkAll my app Crash. LogCat say me: NullPointerException on the line

      CheckBox checkBoxInTheItemList = (CheckBox)
                  mList.getChildAt(i).findViewById(R.id.checkBox);

This is my function:

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_layout_select_friend);  
    checkAll =(CheckBox)findViewById(R.id.checkBoxAll);       
    checkAll.setOnClickListener(new OnClickListener() { 
       @Override
       public void onClick(View arg0) {
          if((checkAll.isChecked())){
           for(int i=1;i<peopleSize;i++){//for each element of my array of Person
             mList=(ListView) findViewById(R.id.list); //This is the id of my List
                                                 //in the list_layout_select_friend
              CheckBox checkBoxInTheItemList = (CheckBox)
              mList.getChildAt(i).findViewById(R.id.checkBox);  //i try to get 
                             //for each item of the List the relative checkBox.                     
        checkBoxInTheItemList.setChecked(true);//Now i checked the checkBox
          }
     }          
    }
    });

}

EDIT: this is my Adapter whith also the method getView

public class NewQAAdapterSelectFriends extends BaseAdapter {
private LayoutInflater mInflater;
private Person[] data;

public NewQAAdapterSelectFriends(Context context) { 
    mInflater = LayoutInflater.from(context);
}

public void setData(Person[] data) {
    this.data = data;
}

@Override
public int getCount() {
    return data.length;
}

@Override
public Object getItem(int item) {
    return data[item];
}

@Override
public long getItemId(int position) {
    return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.item_select_friends, null);
        final ViewHolder viewHolder = new ViewHolder();
        viewHolder.nameText=(TextView) convertView.findViewById(R.id.personName);
        viewHolder.surnameText=(TextView) convertView.findViewById(R.id.personSurname);
        viewHolder.contactImage=(ImageView) convertView.findViewById(R.id.personImage);
        viewHolder.checkBox=(CheckBox)convertView.findViewById(R.id.checkBox);
        convertView.setTag(viewHolder);
        viewHolder.nameText.setTag(viewHolder.nameText);
        viewHolder.nameText.setTag(viewHolder.surnameText);
        viewHolder.contactImage.setTag(data[position]);
        viewHolder.checkBox.setTag(data[position]);// is correct this line??????


    } else {


    }

     ViewHolder holder = (ViewHolder) convertView.getTag();
     holder.nameText.setText(data[position].getName());
     holder.surnameText.setText(data[position].getSurname());
     holder.contactImage.setImageResource(data[position].getPhotoRes());
     holder.contactImage.setScaleType(ScaleType.FIT_XY);
     //here i have to write something for the checkbox, right?
     return convertView;
}

static class ViewHolder {
    TextView nameText;
    TextView surnameText;
    ImageView contactImage;
    CheckBox checkBox;
}

How i can avoid this problem? How i can check all the checkbox of a big list with invisible elements??

  • 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-07T01:37:40+00:00Added an answer on June 7, 2026 at 1:37 am

    In order to implement this, you’ll need an understanding of how ListView work, namely view recycling. When a list is made in Android, the Views are created dynamically. As you scroll up and down, list items that go off the screen need to have their views wiped clean of data and be repopulated with information for the next item coming in. This happens in the getView method in your List Adapter (which, by the way, would be very helpful for you to post). I believe this is your reason for the Null Pointer Exception. The getChildAt method can only return visible list items, because the others do not exist at the moment. The first visible item will be index 0. So even though your list may have 20 items, if only 5 are visible, only 5 can be returned by getChildAt, and asking for a view that’s out of bounds could cause this exception.

    The way I would approach your item is by having a data structure that keeps a record of your list items and the state of the check box. A very simple example of this would be an array of booleans, where the true/false value corresponds to if the box is checked. When getView() is called, you can use the position argument a your as the index to your array, and check or uncheck the box based on this. (You probably should be doing something like this anyway, if you’re recycling your views properly) If the Check All button is pressed, then just make each element of the array true.

    Some sample code to help you out.

    private boolean[] isCheckedArray; //Maintains the checked state of your 
    
    ... //At some point, probably onCreate or whenever you get your data, initialize your array. If nothing every starts checked off, initialize the whole array to false
    
    checkAll.setOnClickListener(new OnClickListener() { 
       @Override
       public void onClick(View arg0){
           for(int i=1;i<peopleSize;i++)
                 isCheckedArray[i] = true;      
       }
    });
    
    ...
    //And finally, in your getView function
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
         ...
         myCheckBox.setChecked(isCheckedArray[position])
         ...
    }
    

    Hope this helps. I’d definitely look into view recycling in Android, as it’s a key concept and having a good handle on it will save you a lot of time and headache down the road. Good luck.

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

Sidebar

Related Questions

I have this situation where I want to display a list of Administration objects
I have this situation where i have to start an activity from my mainActivity.
This is my situation. I have two activities: ONE and TWO. In TWO activity
I have this situation: $(#button).toggle(function(){ $(#window).animate({top:'0%'},1000); },function(){ $(#window).animate({top:'-100%'},1000); }); but I need change it
this is the current state/situation: I have an Activity which binds a Service which
I have situation where I want to change layout of activity after 3 seconds.
I have a difficult situation, let me put it this way, I want the
I have a situation like this I'm using a single activity but with different
I need help with this situation: I have activity, what starts IntentService. Service do
Please, give me a hint how to solve this situation: I have two custom

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.