mao_172985041 2016-05-01 16:35 采纳率: 100%
浏览 1013
已采纳

关于Checbox的OnCheckedChangeListener为什么我写个外部内传控件过来一直空指针,写内部内没问题?

 import java.util.List;
 import android.util.Log;
 import android.widget.CheckBox;
 import android.widget.CompoundButton;
 import android.widget.CompoundButton.OnCheckedChangeListener;

public class Boxlistener  implements OnCheckedChangeListener{
private int type;
private CheckBox cball;
private List<CheckBox> boxs ;
private boolean bool;
public static final int PARENT_BOX=0;
public static final int CHILD_BOX=1;

public Boxlistener(int type) {
    this.type = type;
}

public Boxlistener(int type, CheckBox cball, List<CheckBox> boxs) {
    this.type = type;
    this.cball = cball;
    this.boxs = boxs;
}

@Override
public void onCheckedChanged(CompoundButton buttonView,
        boolean isChecked) {
    switch (type) {
    case PARENT_BOX:
        Log.i("delong","此时父类状态为:"+cball.isChecked());
        cball.setChecked(true);
        if (cball.isChecked()) {
            for (CheckBox box : boxs) {
                box.setChecked(true);
            }
        }else{
            if (!bool) {
                for (CheckBox box : boxs) {
                    box.setChecked(false);
                }
            }else{
                bool=false;
            }
        }
        break;
    case CHILD_BOX:
        Log.i("delong", "当前box状态为:"+cball.isChecked());
        if (cball.isChecked()) {
            Log.e("delong", "当前box状态为:"+isChecked);
            if (isChecked) {

            }else{
                bool = true;
                cball.setChecked(false);
            }
        }else{
            if (isChecked) {
                for (int i=0 ; i<boxs.size();i++) {
                    for(int j= i+1;j<boxs.size();j++){
                        if ((""+boxs.get(j).isChecked()).equals(""+boxs.get(j-1).isChecked())) {
                            if (j==boxs.size()-1) {
                                cball.setChecked(true);
                            }
                        }else{
                            return;
                        }    
                    }
                }
            }
        }
        break;
    }
}
}
  • 写回答

2条回答 默认 最新

  • threenewbee 2016-05-01 20:31
    关注

    调试下,看下具体哪个变量是null的。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 SQL Server下载
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求