无法实施方法

时间:2015-03-30 18:08:22

标签: java android oncreate

我想实现以下方法:

public void setButtons(Button b, int h, int w){
    b.setLayoutParams(new LinearLayout.LayoutParams(w, h));
}

在我的onCreate方法中:

        for (int i = 0; i < buttons.length; i++){
        public void setButtons(buttons[i], widthOfButtons, widthOfButtons);
    }

但是我得到了错误,我一直想弄清楚什么是错的,但我对java很新,我觉得这个代码只需要别人的眼睛所以有人可以告诉我如何正确实现上述方法?感谢。

修改

添加更多代码: 下面是我初始化数组的地方:

   Button[] buttons = new Button[23];
    buttons[0] = (Button) findViewById(R.id.alef);
    buttons[1] = (Button) findViewById(R.id.beis);
    buttons[3] = (Button) findViewById(R.id.gimmel);
    buttons[4] = (Button) findViewById(R.id.daled);
    buttons[5] = (Button) findViewById(R.id.heh);
    buttons[6] = (Button) findViewById(R.id.vav);
    buttons[7] = (Button) findViewById(R.id.zayin);
    buttons[7] = (Button) findViewById(R.id.ches);
    buttons[8] = (Button) findViewById(R.id.tes);
    buttons[9] = (Button) findViewById(R.id.yud);
    buttons[10] = (Button) findViewById(R.id.chaf);
    buttons[11] = (Button) findViewById(R.id.lamed);
    buttons[12] = (Button) findViewById(R.id.mem);
    buttons[13] = (Button) findViewById(R.id.nun);
    buttons[14] = (Button) findViewById(R.id.ayin);
    buttons[15] = (Button) findViewById(R.id.peh);
    buttons[16] = (Button) findViewById(R.id.tzadi);
    buttons[17] = (Button) findViewById(R.id.kuf);
    buttons[18] = (Button) findViewById(R.id.reish);
    buttons[19] = (Button) findViewById(R.id.shin);
    buttons[20] = (Button) findViewById(R.id.taf);
    buttons[21] = (Button) findViewById(R.id.empty1);
    buttons[22] = (Button) findViewById(R.id.empty2);
    buttons[23] = (Button) findViewById(R.id.empty3);

这是我声明方法的完整类:

import android.widget.Button;
import android.widget.LinearLayout;


public class initButtons {

    public void setButtons(Button b, int h, int w){
        b.setLayoutParams(new LinearLayout.LayoutParams(w, h));
    }
}

3 个答案:

答案 0 :(得分:1)

将以下行替换为onCreate

public void setButtons(buttons[i], widthOfButtons, widthOfButtons);

通过

new initButtons().setButtons(buttons[i], widthOfButtons, widthOfButtons);

或者

initButtons iButton = new initButtons();
iButton.setButtons(buttons[i], widthOfButtons, widthOfButtons);

答案 1 :(得分:0)

像这样调用方法

for (int i = 0; i < buttons.length; i++){
            setButtons(buttons[i], widthOfButtons, widthOfButtons);
        }

答案 2 :(得分:0)

你不能在For循环中声明一个函数。 public void setButtons(buttons[i], widthOfButtons, widthOfButtons);是一个声明。此外,你的功能中没有身体。尝试将行更改为setButtons(buttons[i], widthOfButtons, widthOfButtons);并在代码中添加一个函数:

public void setButtons(buttons[i], widthOfButtons, widthOfButtons) {
   doSomething...

}