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 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决