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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:26:50+00:00 2026-06-13T12:26:50+00:00

I am trying to implement the following code: public CheckBox checkboxer() { final CheckBox

  • 0

I am trying to implement the following code:

 public CheckBox checkboxer() {
    final CheckBox box = (CheckBox) findViewById(R.id.cbBox);
    Log.d(LOG_TAG, "make ListView clickable");
    lvMain.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,
               int position, long id) {              
            box.setChecked(true);
        }
    });       
    return box;
}

   private void savebox(final boolean isChecked) {
    SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean("check", isChecked);
    editor.commit();
    Log.d(LOG_TAG, "checkbox is saved");
}

private boolean load() { 
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
return sharedPreferences.getBoolean("check", false);}

When I save it

onPause(){
     savebox(checkboxer().isChecked());
} 

Log indicates that it is saved.
Bun when I put

onResume(){
     checkboxer().setChecked(load());
} 

comes NullPointerException.

I think it is because nothing is checked and saved when activity starts running. Am I correct? How can I go around?

Thank you very much.
here we go

Logcat:

08:18:37.830    1773    example.CustomAdapter   ERROR   AndroidRuntime  FATAL EXCEPTION: main
08:18:37.830    1773    example.CustomAdapter   ERROR   AndroidRuntime  java.lang.RuntimeException: Unable to resume activity {example.CustomAdapter/example.CustomAdapter.ChildActivity}: java.lang.NullPointerException
08:18:37.830    1773    example.CustomAdapter   ERROR   AndroidRuntime      at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2120)
08:18:37.830    1773    example.CustomAdapter   ERROR   AndroidRuntime      at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2135)
08:18:37.830    1773    example.CustomAdapter   ERROR   AndroidRuntime      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1668)
08:18:37.830    1773    example.CustomAdapter   ERROR   AndroidRuntime      at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08:18:37.830    1773    example.CustomAdapter   ERROR   AndroidRuntime      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08:18:37.830    1773    example.CustomAdapter   ERROR   AndroidRuntime      at android.os.Handler.dispatchMessage(Handler.java:99)
08:18:37.830    1773    example.CustomAdapter   ERROR   AndroidRuntime      at android.os.Looper.loop(Looper.java:123)
08:18:37.830    1773    example.CustomAdapter   ERROR   AndroidRuntime      at android.app.ActivityThread.main(ActivityThread.java:3683)
08:18:37.830    1773    example.CustomAdapter   ERROR   AndroidRuntime      at java.lang.reflect.Method.invokeNative(Native Method)
08:18:37.830    1773    example.CustomAdapter   ERROR   AndroidRuntime      at java.lang.reflect.Method.invoke(Method.java:507)
08:18:37.830    1773    example.CustomAdapter   ERROR   AndroidRuntime      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08:18:37.830    1773    example.CustomAdapter   ERROR   AndroidRuntime      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08:18:37.830    1773    example.CustomAdapter   ERROR   AndroidRuntime      at dalvik.system.NativeStart.main(Native Method)
08:18:37.830    1773    example.CustomAdapter   ERROR   AndroidRuntime  Caused by: java.lang.NullPointerException
08:18:37.830    1773    example.CustomAdapter   ERROR   AndroidRuntime      at example.CustomAdapter.ChildActivity.onResume(ChildActivity.java:256)
08:18:37.830    1773    example.CustomAdapter   ERROR   AndroidRuntime      at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150)
08:18:37.830    1773    example.CustomAdapter   ERROR   AndroidRuntime      at android.app.Activity.performResume(Activity.java:3832)
08:18:37.830    1773    example.CustomAdapter   ERROR   AndroidRuntime      at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2110)
08:18:37.830    1773    example.CustomAdapter   ERROR   AndroidRuntime      ... 12 more

Sorry, I forgot. Could it be because in my adapter class I have View method with

 CheckBox cbBuy = (CheckBox) view.findViewById(R.id.cbBox);

    cbBuy.setOnCheckedChangeListener(myCheckChangList);

    cbBuy.setTag(position);

    cbBuy.setChecked(p.box);
    return view;`

And also

  OnCheckedChangeListener myCheckChangList = new OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

      getProduct((Integer) buttonView.getTag()).box = isChecked;
    }
  };

Sorry I am a new guy and probably don’t understand what kind of condradiction could be between checkbox declaration in adapter and in activity. If it is caused because of such contradiciton please explain.

Here is the exact coding for onResume and onPause

@Override
protected void onResume() {
    super.onResume();
    Log.d(LOG_TAG, "ChildActivity: onResume()");
    DataSourceChild.openToWriteChild();
    checkboxer();

}

@Override
protected void onPause() {
    super.onPause();
    Log.d(LOG_TAG, "ChildActivity: onPause()");

    DataSourceChild.closeChild();

    savebox(checkboxer().isChecked());

}

The whole code is too big. Let me provide you with onCreate first` public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.child);

    Log.d(LOG_TAG, "ChildActivity: onCreate()");

    child_datasource = new DataSourceChild(this);
    DataSourceChild.openToWriteChild();

    Intent intent = getIntent();

    val_position = intent.getIntExtra("value_1", pos_1);
    val_id = intent.getLongExtra("value_2", pos_2);

    onSelectionMade();

}


public void onSelectionMade() {

    if (val_position == val_id) {

        loader(val_position);
    }
}`

The loader contains checkboxer and looks as follow` public void loader(int val) {

    item_values = child_datasource.readItem(val);

    // определяем адаптер
    boxAdapter = new BoxAdapter(this, item_values);

    // настраиваем список
    lvMain = (ListView) findViewById(R.id.lvMain);

    // выставляем адаптер
    lvMain.setAdapter(boxAdapter);

    // регистратор контекстного меню
    registerForContextMenu(lvMain);

    // метод для работы с checkbox
    checkboxer();

    boxAdapter.notifyDataSetChanged();
}`

Now in custom BoxAdapter I have the following

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

    View view = convertView;
    if (view == null) {
      view = lInflater.inflate(R.layout.childitem, parent, false);
    }

    Product p = getProduct(position);
    ((TextView) view.findViewById(R.id.tvDescr)).setText(p.name);
    ((TextView) view.findViewById(R.id.tvPrice)).setText(p.price + "");
    ((ImageView) view.findViewById(R.id.ivImage)).setImageResource(p.image);

    CheckBox cbBuy = (CheckBox) view.findViewById(R.id.cbBox);

    cbBuy.setOnCheckedChangeListener(myCheckChangList);

    cbBuy.setTag(position);

    cbBuy.setChecked(p.box);
    return view;
  }`

Sorry for inconvenience caused. But I want to find out what is going on.

Your sample works with one click. Thank you very much. I have tried to update my adapter View method in the following way

`  public View getView(final int position, View convertView, ViewGroup parent) {

    View view = convertView;
    if (view == null) {
      view = lInflater.inflate(R.layout.childitem, parent, false);
    }

    Product p = getProduct(position);

    ((TextView) view.findViewById(R.id.tvDescr)).setText(p.name);
    ((TextView) view.findViewById(R.id.tvPrice)).setText(p.price + "");
    ((ImageView) view.findViewById(R.id.ivImage)).setImageResource(p.image);

    CheckBox cbBuy = (CheckBox) view.findViewById(R.id.cbBox);

    cbBuy.setTag(position);

    cbBuy.setChecked(mIsChecked[position]);

    cbBuy.setOnCheckedChangeListener (new OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

      mIsChecked[position] = isChecked;
      getProduct((Integer) buttonView.getTag()).box = isChecked;
    }
  });

    return view;
  }
  `

and made change in onCreate

 `    public void loader(int val) {

        item_values = child_datasource.readItem(val);

        lvMain = (ListView) findViewById(R.id.lvMain);

        boolean[] isChecked = new boolean[item_values.size()];
        for (int i = 0; i < item_values.size(); i++) {
            isChecked[i] = false;
        }

        boxAdapter = new BoxAdapter(this, item_values, isChecked);

        lvMain.setAdapter(boxAdapter);

        registerForContextMenu(lvMain);

        boxAdapter.notifyDataSetChanged();
    }`

it still can not save checks when I leave ChildActivity. I am sad that I can not apply your example and my knoweledge of Android is not enough for operating with View so easy as you do it. If you can further explain me how should I change View to get result I will appreciate it, if no then sorry for your time.

  • 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-13T12:26:51+00:00Added an answer on June 13, 2026 at 12:26 pm

    When the activity is started onResume() will be called. So in onResume() just give a check before calling setChecked function so that it should execute only when checkboxer() is not null to ensure it being executed only when the activity is resumed.

    onResume(){
         if(checkboxer() != null)
              checkboxer().setChecked(load());
    }
    

    Sample activity with checkbox and listview

    package com.example.checkdoubltap;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.ListView;
    
    public class CheckLayoutParams extends Activity {
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lin);
        ListView list = (ListView) findViewById(R.id.listview);
        boolean[] isChecked = new boolean[countryStrings.length];
        for(int i=0;i<countryStrings.length; i++){
            isChecked[i] = false;
        }
        CheckBoxAdapter adapter = new CheckBoxAdapter(CheckLayoutParams.this, countryStrings, isChecked);
    
        list.setAdapter(adapter);
    
    }
    
    private String[] countryStrings = { "Afghanistan", "Albania",
            "Algeria", "Andorra", "Angola", "Anguilla", "Antigua and Barbuda",
            "Argentina", "Armenia", "Aruba", "Ascension Island", "Australia",
            "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh",
            "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bermuda",
            "Bhutan", "Bolivia", "Bosnia and Herzegovina", "Botswana",
            };
    }
    

    Sample adapter:

    package com.example.checkdoubltap;
    
      import android.content.Context;
      import android.util.Log;
      import android.view.View;
      import android.view.ViewGroup;
      import android.widget.BaseAdapter;
      import android.widget.CheckBox;
      import android.widget.CompoundButton;
      import android.widget.CompoundButton.OnCheckedChangeListener;
    
      public class CheckBoxAdapter extends BaseAdapter {
    
    private Context mContext;
    private String[] mCountries;
    private boolean[] mIsChecked;
    
    public CheckBoxAdapter(Context context, String[] countryStrings, boolean[] isChecked) {
        mContext = context;
        mCountries = countryStrings;
        mIsChecked = isChecked;
    }
    
    @Override
    public int getCount() {
        if(mCountries != null)
            return mCountries.length;
        return 0;
    }
    
    @Override
    public Object getItem(int position) {
        return null;
    }
    
    @Override
    public long getItemId(int position) {
        return 0;
    }
    
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
    
        CheckBox view = new CheckBox(mContext);
        view.setChecked(load(position));
    //  view.setChecked(mIsChecked[position]);
        view.setText(mCountries[position]);
        view.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Log.d("Check","Changing position to"+position+" "+isChecked);
                mIsChecked[position] = isChecked;
                savebox(position,isChecked);
            }
        });
        return view;
    }
    
    private boolean load(int position) { 
        SharedPreferences sharedPreferences = ((Activity) mContext).getPreferences(Context.MODE_PRIVATE);
        return sharedPreferences.getBoolean(""+position, false);
    }
    
    
    
    private void savebox(int position, final boolean isChecked) {
        SharedPreferences sharedPreferences = ((Activity) mContext).getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(""+position, isChecked);
        editor.commit();
        Log.d(TAG, "checkbox is saved");
    }
      }
    

    Layout with listview

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    
    <ListView 
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:id="@+id/listview"
    >
    </ListView>
    </LinearLayout>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement autocomplete for a textbox using the following code, but it's
I'm trying to implement the example code of the following question by using opencv
I'm trying to implement http://github.com/bengottlieb/Twitter-OAuth-iPhone/tree/master into my code. I'm following the Demo provided and
I'm trying to compile the following code: public class BaseRequest<TResponse> where TResponse : BaseResponse
I am trying to implement a linked list using following code, I got segment
I'm trying to implement a cache using Zend Cache. I use the following code
I have implement the following code to create a SSL server socket. public void
I am trying to convert the following code for the variance calculation public static
I am trying to implement the following code to map a database to a
I was trying to implement JNA. I have used the following code for the

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.