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();
}
}
});
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.ViewHolder> {
DoctorsModel list;
Context context;
Activity activity;
ChangeFragments changeFragments;
public UserAdapter(DoctorsModel list ,Context context, Activity activity) {
this.list = list;
this.context = context;
this.activity = activity;
changeFragments = new ChangeFragments(context);
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.usersitemlayout, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
holder.userName.setText(list.getDoctors().get(position).getFullName());
Picasso.get().load(list.getDoctors().get(position).getImage().getUrl()).into(holder.userImage);
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();
}
}
});
}
@Override
public int getItemCount() {
return list.getDoctors().size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView userName;
CircleImageView userImage;
CheckBox ChechKadin,ChechErkek;
public ViewHolder(View itemView) {
super(itemView);
userName = itemView.findViewById(R.id.userName);
userImage = itemView.findViewById(R.id.userImage);
ChechKadin = itemView.findViewById(R.id.CheckKadin);
ChechErkek = itemView.findViewById(R.id.CheckErkek);
}
}
}
public class UsersFragment extends Fragment {
private View view;
private ChangeFragments changeFragments;
private RecyclerView kullaniciRecylerView;
private DoctorsModel list;
private UserAdapter userAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_users, container, false);
tanimla();
getKullaniciler();
return view; }
public void tanimla()
{
changeFragments = new ChangeFragments(getContext());
kullaniciRecylerView = view.findViewById(R.id.userRecylerView);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getContext(),1);
kullaniciRecylerView.setLayoutManager(layoutManager);
list = new DoctorsModel();
}
public void getKullaniciler()
{
Call<DoctorsModel> req = ManagerAll.getInstance().getUser();
req.enqueue(new Callback<DoctorsModel>() {
@Override
public void onResponse(Call<DoctorsModel> call, Response<DoctorsModel> response) {
// list = (DoctorsModel) response.body().getDoctors();
list = response.body();
userAdapter = new UserAdapter(list,getContext(),getActivity());
Log.i("kullaniciler",response.body().toString());
kullaniciRecylerView.setAdapter(userAdapter);
}
@Override
public void onFailure(Call<DoctorsModel> call, Throwable t) {
Toast.makeText(getContext(), "HATAA:...!!", Toast.LENGTH_LONG).show();
}
});
}
}
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);