I want to list by gender from Json with CheckBox, but the error I get is as follows: Attempt to invoke virtual method ‘void android.widget.CheckBox.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)’ on a null object reference
holder.ChechKadin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (holder.ChechKadin.isChecked()){
Toast.makeText(context, "True", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(context, "False", Toast.LENGTH_SHORT).show();
}
}
});
2
Answers
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.ViewHolder> {
}
public class UsersFragment extends Fragment {
}
Please share the class code (Activity or adapter) where you are using the check box. As per by the logs, it looks like initialization issue. If you are using inside the adapter class then inside View Holder
this.ChechKadin = (CheckBox) itemView.findViewById(R.id.ChechKadin);
// Check if you have initialized the checkbox.if you are using the check box inside the activity
ChechKadin =(CheckBox) findViewById(R.id.ChechKadin);