11

我想将按钮映射到按钮数组,并且代码在编译时没有错误,但是当我运行它时会强制关闭:

Button buttons[];

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game_board_view);

    // Set OnClick listeners
    Button buttons[] = null; 
    buttons[0] = (Button)findViewById(R.id.buttonOne);
    buttons[1] = (Button)findViewById(R.id.buttonTwo);
    buttons[2] = (Button)findViewById(R.id.buttonThree);
    buttons[3] = (Button)findViewById(R.id.buttonFour);
    buttons[4] = (Button)findViewById(R.id.buttonFive);
    buttons[5] = (Button)findViewById(R.id.buttonSix);
    buttons[6] = (Button)findViewById(R.id.buttonSeven);
    buttons[7] = (Button)findViewById(R.id.buttonEight);
    buttons[8] = (Button)findViewById(R.id.buttonMid);
}

日志猫:

03-26 21:42:51.455: D/dalvikvm(1156): GC_EXTERNAL_ALLOC freed 55K, 53% free 2566K/5379K, external 1625K/2137K, paused 98ms
03-26 21:42:54.323: D/AndroidRuntime(1156): Shutting down VM
03-26 21:42:54.323: W/dalvikvm(1156): threadid=1: thread exiting with uncaught exception (group=0x40015560)
03-26 21:42:54.343: E/AndroidRuntime(1156): FATAL EXCEPTION: main
03-26 21:42:54.343: E/AndroidRuntime(1156): java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.project.superwordwheel/edu.project.superwordwheel.GameView}: java.lang.NullPointerException
03-26 21:42:54.343: E/AndroidRuntime(1156):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-26 21:42:54.343: E/AndroidRuntime(1156):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-26 21:42:54.343: E/AndroidRuntime(1156):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-26 21:42:54.343: E/AndroidRuntime(1156):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-26 21:42:54.343: E/AndroidRuntime(1156):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-26 21:42:54.343: E/AndroidRuntime(1156):     at android.os.Looper.loop(Looper.java:123)
03-26 21:42:54.343: E/AndroidRuntime(1156):     at android.app.ActivityThread.main(ActivityThread.java:3683)
03-26 21:42:54.343: E/AndroidRuntime(1156):     at java.lang.reflect.Method.invokeNative(Native Method)
03-26 21:42:54.343: E/AndroidRuntime(1156):     at java.lang.reflect.Method.invoke(Method.java:507)
03-26 21:42:54.343: E/AndroidRuntime(1156):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-26 21:42:54.343: E/AndroidRuntime(1156):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-26 21:42:54.343: E/AndroidRuntime(1156):     at dalvik.system.NativeStart.main(Native Method)
03-26 21:42:54.343: E/AndroidRuntime(1156): Caused by: java.lang.NullPointerException
03-26 21:42:54.343: E/AndroidRuntime(1156):     at edu.project.superwordwheel.GameView.onCreate(GameView.java:43)
03-26 21:42:54.343: E/AndroidRuntime(1156):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-26 21:42:54.343: E/AndroidRuntime(1156):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-26 21:42:54.343: E/AndroidRuntime(1156):     ... 11 more
4

6 回答 6

30

如果您不必将像 9 这样的常量硬编码到代码中,通常会更好。而且你通常不需要。

例如,您可以将 id 放入一个数组并List根据它们构建一个动态大小的

private List<Button> buttons;
private static final int[] BUTTON_IDS = {
    R.id.buttonOne,
    R.id.buttonTwo, 
    R.id.buttonThree,
    R.id.buttonFour,
    R.id.buttonFive,
    R.id.buttonSix, 
    R.id.buttonSeven,
    R.id.buttonEight,
    R.id.buttonMid,
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game_board_view);

    buttons = new ArrayList<Button>();
    // or slightly better
    // buttons = new ArrayList<Button>(BUTTON_IDS.length);
    for(int id : BUTTON_IDS) {
        Button button = (Button)findViewById(id);
        button.setOnClickListener(this); // maybe
        buttons.add(button);
    }
}
于 2013-03-26T16:32:37.493 回答
6

您的数组为空,您正在尝试获取其中的索引。这就是导致NullPointerException. 必须先初始化数组,然后才能使用它来存储按钮。

如果您想要一个包含九个按钮的数组,请更改此行:

Button buttons[] = null; 

对此:

Button buttons[] = new Button[9];

此外,您有一个类成员Button buttons[]和一个也名为Button buttons[]. 如果这是故意的,那么一定要继续。否则,您需要进一步将您的行更改为:

buttons[] = new Button[9];
于 2013-03-26T16:20:39.630 回答
3

示例用法:

Button[] buttons = initializeButtons(3);
buttons[1].setText("I am button1");
buttons[2].setText("I am button2");
buttons[3].setText("I am button3");

功能:

public Button[] initializeButtons(int x) {
    Resources res = getResources();
    Button[] buttons = new Button[x];
    for (int i = 0; i < x; i++) {
        String b = "button" + i;
        buttons[i] = (Button) findViewById(res.getIdentifier(b, "id", getPackageName()));
    } return buttons;//to prevent array out of bounds exception start from 0
}

注意:确保在您的布局中按钮 ID 为 button1、button2、button3、.. .. 等

于 2015-01-13T13:05:18.403 回答
3
 Button buttons[] = null; 

必须使用new运算符创建按钮:

 Button buttons[] = new Button[9];
于 2013-03-26T16:20:25.083 回答
2

试试下面的代码:

private int objectLength = 9; //Array elements 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game_board_view);

    Button[] buttons = new Button[objectLength]; 
    buttons[0] = (Button)findViewById(R.id.buttonOne);
    buttons[1] = (Button)findViewById(R.id.buttonTwo);
    buttons[2] = (Button)findViewById(R.id.buttonThree);
    buttons[3] = (Button)findViewById(R.id.buttonFour);
    buttons[4] = (Button)findViewById(R.id.buttonFive);
    buttons[5] = (Button)findViewById(R.id.buttonSix);
    buttons[6] = (Button)findViewById(R.id.buttonSeven);
    buttons[7] = (Button)findViewById(R.id.buttonEight);
    buttons[8] = (Button)findViewById(R.id.buttonMid);
}
于 2013-03-26T16:21:47.543 回答
0

我有这样的情况,我选择了不同的方法。我将 id 存储到整数数组中。

Int[] btnarr = new int[3];

btn[0]=R.id.button1; // give your ID 

Button btn = (Button).findViewById(cc[i]);
于 2017-02-25T07:50:44.213 回答