表示子控件索引是从0开始 你把log拿出来看下错误把 

解决方案 »

  1.   


    大神,图在次,我看不懂java的报错
      

  2.   

    转型异常了 你看下LinearLayout ll01 = (LinearLayout)findViewById(R.id.LinearLayout01); 是否获取正确 是否是checkbox的直接父控件 
      

  3.   

    我是采用代码循环添加的方式,源码如下
    String deptCode = Tools.ReadXml(xml, "deptcodelist");
    String deptName = Tools.ReadXml(xml, "deptnamelist");
    String[] deptCodeList;// 部门编码集合
    String[] deptNameList;// 部门名称集合
    deptCodeList = deptCode.split(",");
    deptNameList = deptName.split(",");
    LinearLayout ll = (LinearLayout) this
    .findViewById(R.id.LinearLayout01);
    for (int i = 0; i < deptCodeList.length; i++) {
    LinearLayout checkboxLinearLayout = (LinearLayout) getLayoutInflater()
    .inflate(R.layout.checkboxdoitem, null); // 获得checkbox.xml文件中的CheckBox对象
    CheckBox cbItem = (CheckBox) checkboxLinearLayout
    .findViewById(R.id.checkBoxItem1);
    cbItem.setTag(deptCodeList[i]);
    cbItem.setText(deptNameList[i]);
    ll.addView(checkboxLinearLayout, i);
    }
    然后我修改代码为
    LinearLayout ll01 = (LinearLayout) findViewById(R.id.LinearLayout01);
    //循环赋值
    for(int i=1;i<=ll01.getChildCount();i++)
    {
    LinearLayout llItem=(LinearLayout)ll01.getChildAt(i);
    CheckBox cbItem=(CheckBox)llItem.getChildAt(i);
    cbItem.setChecked(cbItemCheck);
    }
    报错log如下图
      

  4.   

    for(int i=1;i<=ll01.getChildCount();i++)应该是for(int i=0;i<ll01.getChildCount();i++)
      

  5.   

    我把i=0改了,忘了改i<=,所以有错,目前好用了
      

  6.   

    问题出在这里LinearLayout checkboxLinearLayout = (LinearLayout) getLayoutInflater()
     .inflate(R.layout.checkboxdoitem, null); // 获得checkbox.xml文件中的CheckBox对象
    CheckBox cbItem = (CheckBox) checkboxLinearLayout
     .findViewById(R.id.checkBoxItem1);
     cbItem.setTag(deptCodeList[i]);
     cbItem.setText(deptNameList[i]);
     ll.addView(checkboxLinearLayout, i);
    你往II中添加的是LinearLayout,而不是CheckBox,当然无法将LinearLayout强制转换为CheckBox
      

  7.   

    我那种循环添加checkbox的方式,如果部门太多,直接有的就不见了,求教,LinearLayout不是会自适应高度么
      

  8.   

    我是采用代码循环添加的方式,源码如下
    String deptCode = Tools.ReadXml(xml, "deptcodelist");
    String deptName = Tools.ReadXml(xml, "deptnamelist");
    String[] deptCodeList;// 部门编码集合
    String[] deptNameList;// 部门名称集合
    deptCodeList = deptCode.split(",");
    deptNameList = deptName.split(",");
    LinearLayout ll = (LinearLayout) this
    .findViewById(R.id.LinearLayout01);
    for (int i = 0; i < deptCodeList.length; i++) {
    LinearLayout checkboxLinearLayout = (LinearLayout) getLayoutInflater()
    .inflate(R.layout.checkboxdoitem, null); // 获得checkbox.xml文件中的CheckBox对象
    CheckBox cbItem = (CheckBox) checkboxLinearLayout
    .findViewById(R.id.checkBoxItem1);
    cbItem.setTag(deptCodeList[i]);
    cbItem.setText(deptNameList[i]);
    ll.addView(checkboxLinearLayout, i);
    }
    然后我修改代码为
    LinearLayout ll01 = (LinearLayout) findViewById(R.id.LinearLayout01);
    //循环赋值
    for(int i=1;i<=ll01.getChildCount();i++)
    {
    LinearLayout llItem=(LinearLayout)ll01.getChildAt(i);
    CheckBox cbItem=(CheckBox)llItem.getChildAt(i);
    cbItem.setChecked(cbItemCheck);
    }
    报错log如下图

    151行报空指针异常 
      

  9.   

    我那种循环添加checkbox的方式,如果部门太多,直接有的就不见了,求教,LinearLayout不是会自适应高度么
    我的xml所有的域都是LinearLayout,为什么还是变成这样
      

  10.   

    我那种循环添加checkbox的方式,如果部门太多,直接有的就不见了,求教,LinearLayout不是会自适应高度么
    LinearLayout  高度设置为 android:layout_height="wrap_content"
      

  11.   

    兄弟们,别纠结前面了,是我自己2了,没注意套用了LinearLayout,帮忙解决这个我动态添加checkbox的时候,高度问题
      

  12.   

    我那种循环添加checkbox的方式,如果部门太多,直接有的就不见了,求教,LinearLayout不是会自适应高度么
    是会自适应高度,但是如果LinearLayout不是可滚动的话,超过手机屏幕的内容就不会显示了。
      

  13.   

    我那种循环添加checkbox的方式,如果部门太多,直接有的就不见了,求教,LinearLayout不是会自适应高度么
    是会自适应高度,但是如果LinearLayout不是可滚动的话,超过手机屏幕的内容就不会显示了。你的意思是换行?例如每4个一行, 我判断if(i%4)给checkbox添加什么属性呢?我一直没解决这个
      

  14.   

    你这是宽度问题 不是高度问题-.- 外面套个HorizontalScrollView把 
      

  15.   

    我那种循环添加checkbox的方式,如果部门太多,直接有的就不见了,求教,LinearLayout不是会自适应高度么
    我的xml所有的域都是LinearLayout,为什么还是变成这样

    LinearLayout的默认布局是横向的,你设置成垂直方向的就是,android:orientation="vertical"
      

  16.   

    我那种循环添加checkbox的方式,如果部门太多,直接有的就不见了,求教,LinearLayout不是会自适应高度么
    是会自适应高度,但是如果LinearLayout不是可滚动的话,超过手机屏幕的内容就不会显示了。你的意思是换行?例如每4个一行, 我判断if(i%4)给checkbox添加什么属性呢?我一直没解决这个
    最外层一个LinearLayout,设置为垂直布局,然后每4个一行放在一个LinearLayout,这个LinearLayout设置为水平布局。