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条)

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮