Tableau de Boutons dans Android

Je veux mapper les boutons d'un tableau de boutons et le code ne contient pas d'erreurs lors de la compilation, mais il y a la force de fermeture quand je le lance:

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
vous avez d'erreur dans la ligne 43
Mettre Button[] buttons = new Buttons[9]; au lieu de Button buttons[] = null. Votre référence à la matrice est nulle.
essayez d'utiliser arraylist<Button> ou définir ur tableau de taille comme = new Bouton[9];
je suis un génie 😛 nan sérieusement, logcat dit cela, cochez la case de cette ligne à l'edu.projet.superwordwheel.GameView.onCreate(GameView.java:43).
Je sais que c'est très ennuyeux à lire toutes ces lignes, mais généralement la lecture de logcat est de 1/2 de la réponse. 🙂 bonne chance, j'espère que vous devenir pro débogueur 😉

OriginalL'auteur Mach Mitch | 2013-03-26