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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:57:31+00:00 2026-06-03T15:57:31+00:00

I have an activity that has a checkbox, then under it is a list

  • 0

I have an activity that has a checkbox, then under it is a list of contacts populating my listview with checkboxes for each contact. I have two major problems stemming from my question.

  1. chkbox_foo is outside of the listview, and chk_bar is inside. chk_foo works, but anything related to chk_bar after being initialized causes the app to crash. Also, if I create a setOnCheckedChangeListener for chkbox_bar, that will cause the app to crash also. Does anyone know why this is happening and how I can fix this?

    btn_foo = (Button) findViewById(R.id.btn_foo);
    barList = (ListView) findViewById(R.id.lv_barList);
    chk_foo = (CheckBox) findViewById(R.id.cb_foo);
    chk_bar = (CheckBox) findViewById(R.id.cb_bar);
    
    // set checkboxes state as false at beginning
    chkboxAllVisible = false;
    chkboxSingleChk = false;
    
    chk_foo.setChecked(chkboxAllVisible);
    chk_bar.setChecked(chkboxChk);  <---App crashes here
    
    // Outside of listview checkbox 
    chk_foo.setOnCheckedChangeListener(new OnCheckedChangeListener(){
    
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            Log.d(TAG, "checkbox changed " + isChecked);
            if(isChecked){
                chkboxAllVisible = true;
                chk_bar.setChecked(isChecked); <---app crashes here too
            } 
        }
    });
     // Outside of listview checkbox 
    chk_bar.setOnCheckedChangeListen... <---app crashes here also
    
  • 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-03T15:57:32+00:00Added an answer on June 3, 2026 at 3:57 pm

    chk_bar is probably null. You can’t call findViewByID on the main layout if the checkbox is inside a listview. You have to call it on the layout where it is housed/contained, which would be the listview item.

    That said, you don’t need to do that. If you have contacts with checkboxes, I’m guessing you are using some sort of custom adapter? If so, good, then it this will be easy. I’m also assuming you are keeping track of whether or not an item/contact has been checked (if not, you’ll need to implement that anyways, see tutorial below)

    In your getView of your custom adapter, while generating the view, you need to look at the data object and if the IsChecked value is true, then you check the checkbox.

    public View getView(int position, View convertView, ViewGroup parent)
    {
        //Inflate your view
        //View listViewLayout = ...
    
        //Here is where you get the reference to the checkbox
        Checkbox chkBox = (CheckBox)listViewLayout.findViewById(R.id.cb_bar);
    
        //Get the data object from whatever your source is (maybe an array list?)
        SomeObj curObj = (SomeObj)listOfItems.get(position);
    
        chkBox.setChecked(curObj.IsChecked);
    }
    

    Here is a full tutorial on how to implement a custom Adapter for Listview:
    http://www.ezzylearning.com/tutorial.aspx?tid=1763429&q=customizing-android-listview-items-with-custom-arrayadapter

    Once you have that working, the rest is simple:

    When the user clicks the master checkbox:

    1. Regenerate the list of items but force all the checkbox values to true.
    2. Call adapter.notifyDataSetChanged()

    When you call notifyDataSetChanged() the listview will re-draw itself (if it believes the source items have in fact been changed. See this: notifyDataSetChanged example)

    When it regenerates the listview, it will iterate through your source items you passed in, and since you regenerated the list and marked all items as “IsChecked” (or whatever your variable name is), it will check each checkbox.

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

Sidebar

Related Questions

I have an activity that has two tabs and a list view in each.
I have an app that has a list as its main activity and then
Assume that we have 3 different activities. Each activity has its own XML file
I have an activity that has two layouts one in the folder layout and
I have an activity that has a TabHost containing a set of TabSpecs each
I have an activity that has 12 buttons, and each one plays a separate
I have a listview that needs to be multiple choice (i.e each list item
I have an activity that has two EditText boxes, one email and one plain
I have an Activity class which has an attribute that references an AsyncTask instance
So I have my main activity that has some views and I have an

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.