【问题标题】:Android Studio CheckBox ProbleAndroid Studio CheckBox 问题
【发布时间】:2021-10-05 23:39:16
【问题描述】:

我想用 CheckBox 从 Json 中按性别列出,但我得到的错误如下: Attempt to invoke virtual method 'void android.widget.CheckBox.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)' on a null对象引用

        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();

                }
            }
        });

【问题讨论】:

    标签: java android json android-studio


    【解决方案1】:

    公共类 UserAdapter 扩展 RecyclerView.Adapter {

    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);
    
        }
    }
    

    }

    公共类用户片段扩展片段{

    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();
    
            }
        });
    }
    

    }

    【讨论】:

      【解决方案2】:

      请分享您使用复选框的类代码(活动或适配器)。根据日志,它看起来像初始化问题。如果您在适配器类中使用,那么在 View Holder 中使用

      this.ChechKadin = (CheckBox) itemView.findViewById(R.id.ChechKadin); // 检查是否初始化了复选框。

      如果您使用的是活动内的复选框

      ChechKadin =(CheckBox) findViewById(R.id.ChechKadin);

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-08
        • 1970-01-01
        • 2014-02-15
        • 2014-06-12
        • 1970-01-01
        相关资源
        最近更新 更多