未选中列表视图中的所有复选框

如果我向其中添加超过13个项目,则Checkbox(listview)无法正常工作。它适用于13个项目或少于此项目但不超过此项目。它给出nullpointerexception和应用程序崩溃。我在这里阅读了很多答案,但没有任何帮助。请让我知道我需要做些什么更改。 这是我的活动课。

public class CheckboxActivity extends AppCompatActivity {
ArrayList<CheckboxPojo> list;
ListView list_checkbox ;
CheckboxAdapter adapter;
CheckBox tv_select,cb1;
ViewGroup listItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_checkbox);

    list = new ArrayList<CheckboxPojo>();
    list_checkbox = (ListView)findViewById(R.id.list_checkbox);
    adapter = new CheckboxAdapter(this,list);
    list_checkbox.setAdapter(adapter);

    list.add(new CheckboxPojo(cb1,"a1"));
    list.add(new CheckboxPojo(cb1,"a2"));
    list.add(new CheckboxPojo(cb1,"a3"));
    list.add(new CheckboxPojo(cb1,"a4"));
    list.add(new CheckboxPojo(cb1,"a5"));
    list.add(new CheckboxPojo(cb1,"a6"));
    list.add(new CheckboxPojo(cb1,"a7"));
    list.add(new CheckboxPojo(cb1,"a8"));
    list.add(new CheckboxPojo(cb1,"a9"));
    list.add(new CheckboxPojo(cb1,"a10"));
    list.add(new CheckboxPojo(cb1,"a11"));
    list.add(new CheckboxPojo(cb1,"a12"));
    list.add(new CheckboxPojo(cb1,"a13"));
    list.add(new CheckboxPojo(cb1,"a14"));

    tv_select = findViewById(R.id.tv_select);
    tv_select.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(tv_select.isChecked()){
                tv_select.setText("Unselect All");
                for ( int i=0; i < list_checkbox.getAdapter().getCount(); i++) {

                     listItem = (ViewGroup)list_checkbox.getChildAt(i);
                     cb1 = (CheckBox) listItem.findViewById(R.id.cb1);
                     cb1.setChecked(true);
                }
            }
            if(!tv_select.isChecked()){
                tv_select.setText("Select All");

                for ( int i=0; i < list_checkbox.getAdapter().getCount(); i++) {
                     listItem = (ViewGroup)list_checkbox.getChildAt(i);
                     cb1 = (CheckBox) listItem.findViewById(R.id.cb1);
                     cb1.setChecked(false);
                }
            }
        }
    });
}}

日志猫

流程:com.example.gridlist,PID:21282     java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ android.view.View android.view.ViewGroup.findViewById(int)”         在com.example.gridlist.Activity.CheckboxActivity $ 1.onClick(CheckboxActivity.java:57)         在android.view.View.performClick(View.java:6608)         在android.widget.CompoundButton.performClick(CompoundButton.java:134)         在android.view.View.performClickInternal(View.java:6585)         在android.view.View.access $ 3100(View.java:778)         在android.view.View $ PerformClick.run(View.java:25944)         在android.os.Handler.handleCallback(Handler.java:873)         在android.os.Handler.dispatchMessage(Handler.java:99)         在android.os.Looper.loop(Looper.java:193)         在android.app.ActivityThread.main(ActivityThread.java:6862)         在java.lang.reflect.Method.invoke(本机方法)         在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:504)         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:870) 2020-02-07 00:38:22.439 21222-21631 /? E / PowerHintCallback:sceneId:0无效

评论