我需要用到很多Button控件,所以想定义一个数组。
先声明了
Button []button;
再取得Button对象时就报错,是不是要先new一个对象?
 button[0]=(Button)findViewById(R.id.btton01);
 button[1]=(Button)findViewById(R.id.button01_1);
 button[2]=(Button)findViewById(R.id.button01_2);
 button[3]=(Button)findViewById(R.id.button01_3);
 button[4]=(Button)findViewById(R.id.button01_4);

解决方案 »

  1.   

    findViewById 找的是Layout已经有的,如果没有,肯定找不出来啊
      

  2.   

    先new一个button[0]=new Button()
      

  3.   


    是需要先new一个button对象。
      

  4.   

    如果layout里面你写了button的话可以findviewbyid
    没写的话,就必须new一个button了
      

  5.   

    还是一个一个按钮定义比较保险,去一些android技术论坛看看,应该可以找到很多资料,www.connectionwe.com安卓航班网
      

  6.   

    肯定得先给button数组new一个对象,并且设置一个大小,然后再通过button[index]进行赋值
      

  7.   

    弄个ArrayList<Button> mlist;不就行了?
      

  8.   


    private ArrayList<Button> mlist = new List<Button>();
    mlist.add((Button)findViewById(R.id.btton01));
    mlist.add((Button)findViewById(R.id.button01_1));
    mlist.add((Button)findViewById(R.id.button01_2));
    mlist.add((Button)findViewById(R.id.button01_3));
    mlist.add((Button)findViewById(R.id.button01_4));
      

  9.   

    Button []button;都没分配空间你怎么给它赋值?Button[] button = new Button[4]