Android中的按钮数组 [英] Array of Buttons in Android

查看:76
本文介绍了Android中的按钮数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to map the buttons to an array of buttons and the code has no errors while compiling but there is force close when i run it:

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);
}

LogCat:

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

推荐答案

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

Your array is null and you're trying to get an index into it. That is what's causing the NullPointerException. Your array must be initialized before you can use it to store your buttons.

如果要由九个按钮组成的数组,请更改此行:

If you want an array of nine buttons then change this line:

Button buttons[] = null; 

对此:

Button buttons[] = new Button[9];

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

Also, you have a class member Button buttons[] and a local function variable that is also named Button buttons[]. If this is intentional then by all means carry on. Otherwise, you'll want to further change your line to this:

buttons[] = new Button[9];

这篇关于Android中的按钮数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆