Different sound on each button

395 views Asked by At

I have an array of the buttons and I want to play different sound by clicking on each button. When I click at some buttons everything is OK, but when I click on other buttons, application crashes, and logcat says:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference

This is sample of my code:

public class HardActivity extends ActionBarActivity {

private Button BoardButtons[][];
private MediaPlayer[] playAudio = new MediaPlayer[49];

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

    BoardButtons = new Button[Game.getBOARD_SIZE_VERTICAL_MEDIUM()][Game.getBOARD_SIZE_HORIZONTAL_MEDIUM()];
    BoardButtons[0][0] = (Button)findViewById(R.id.button1);
    BoardButtons[0][1] = (Button)findViewById(R.id.button2);
    BoardButtons[0][2] = (Button)findViewById(R.id.button3);
    BoardButtons[0][3] = (Button)findViewById(R.id.button4);
    BoardButtons[0][4] = (Button)findViewById(R.id.button5);
    BoardButtons[0][5] = (Button)findViewById(R.id.button6);
    BoardButtons[0][6] = (Button)findViewById(R.id.button7);
    BoardButtons[1][0] = (Button)findViewById(R.id.button8);
    BoardButtons[1][1] = (Button)findViewById(R.id.button9);
    BoardButtons[1][2] = (Button)findViewById(R.id.button10);
    BoardButtons[1][3] = (Button)findViewById(R.id.button11);
    BoardButtons[1][4] = (Button)findViewById(R.id.button12);
    BoardButtons[1][5] = (Button)findViewById(R.id.button13);
    BoardButtons[1][6] = (Button)findViewById(R.id.button14);
    BoardButtons[2][0] = (Button)findViewById(R.id.button15);
    BoardButtons[2][1] = (Button)findViewById(R.id.button16);
    BoardButtons[2][2] = (Button)findViewById(R.id.button17);
    BoardButtons[2][3] = (Button)findViewById(R.id.button18);
    BoardButtons[2][4] = (Button)findViewById(R.id.button19);
    BoardButtons[2][5] = (Button)findViewById(R.id.button20);
    BoardButtons[2][6] = (Button)findViewById(R.id.button21);
    BoardButtons[3][0] = (Button)findViewById(R.id.button22);
    BoardButtons[3][1] = (Button)findViewById(R.id.button23);
    BoardButtons[3][2] = (Button)findViewById(R.id.button24);
    BoardButtons[3][3] = (Button)findViewById(R.id.button25);
    BoardButtons[3][4] = (Button)findViewById(R.id.button26);
    BoardButtons[3][5] = (Button)findViewById(R.id.button27);
    BoardButtons[3][6] = (Button)findViewById(R.id.button28);
    BoardButtons[4][0] = (Button)findViewById(R.id.button29);
    BoardButtons[4][1] = (Button)findViewById(R.id.button30);
    BoardButtons[4][2] = (Button)findViewById(R.id.button31);
    BoardButtons[4][3] = (Button)findViewById(R.id.button32);
    BoardButtons[4][4] = (Button)findViewById(R.id.button33);
    BoardButtons[4][5] = (Button)findViewById(R.id.button34);
    BoardButtons[4][6] = (Button)findViewById(R.id.button35);
    BoardButtons[5][0] = (Button)findViewById(R.id.button36);
    BoardButtons[5][1] = (Button)findViewById(R.id.button37);
    BoardButtons[5][2] = (Button)findViewById(R.id.button38);
    BoardButtons[5][3] = (Button)findViewById(R.id.button39);
    BoardButtons[5][4] = (Button)findViewById(R.id.button40);
    BoardButtons[5][5] = (Button)findViewById(R.id.button41);
    BoardButtons[5][6] = (Button)findViewById(R.id.button42);
    BoardButtons[6][0] = (Button)findViewById(R.id.button43);
    BoardButtons[6][1] = (Button)findViewById(R.id.button44);
    BoardButtons[6][2] = (Button)findViewById(R.id.button45);
    BoardButtons[6][3] = (Button)findViewById(R.id.button46);
    BoardButtons[6][4] = (Button)findViewById(R.id.button47);
    BoardButtons[6][5] = (Button)findViewById(R.id.button48);
    BoardButtons[6][6] = (Button)findViewById(R.id.button49);

    Game = new FieldsGame();
    startNewGame();

    setVolumeControlStream(AudioManager.STREAM_MUSIC);
    playAudio[0] = MediaPlayer.create(this, R.raw.dry_kick);
    playAudio[1] = MediaPlayer.create(this, R.raw.kick_drum);
    playAudio[2] = MediaPlayer.create(this, R.raw.simplekick);
    playAudio[3] = MediaPlayer.create(this, R.raw.kick_q_shot);
    playAudio[4] = MediaPlayer.create(this, R.raw.kick_bass);
    playAudio[5] = MediaPlayer.create(this, R.raw.kick_synth);
    playAudio[6] = MediaPlayer.create(this, R.raw.kick_hot);
    playAudio[7] = MediaPlayer.create(this, R.raw.simplesnare4);
    playAudio[8] = MediaPlayer.create(this, R.raw.snare2);
    playAudio[9] = MediaPlayer.create(this, R.raw.snare_cool);
    playAudio[10] = MediaPlayer.create(this, R.raw.snare_soft);
    playAudio[11] = MediaPlayer.create(this, R.raw.clap_bell);
    playAudio[12] = MediaPlayer.create(this, R.raw.clap_hot);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

private void startNewGame() {
    Game.clearBoard();
    int[] randomVer = new int[6];
    int[] randomHor = new int[6];
    for(int i = 0;i < 6; i++ ) {
        randomVer[i] = rand.nextInt(6) + 0;
        randomHor[i] = rand.nextInt(6) + 0;
    }
    for (int i = 0; i < BoardButtons.length; i++) {
        for(int j = 0; j < BoardButtons[i].length; j++) {
            String buttonText = BoardButtons[i][j].getText().toString();
            if (i == randomVer[0] && j == randomHor[0] && j != BoardButtons[i].length)
                BoardButtons[i][j].setText(Game.DIRECTION_RIGHT);
            else if (i == randomVer[1] && j == randomHor[1] && i != BoardButtons.length)
                BoardButtons[i][j].setText(Game.DIRECTION_DOWN);
            else if (i == randomVer[2] && j == randomHor[2] && j != BoardButtons[i].length)
                BoardButtons[i][j].setText(Game.DIRECTION_RIGHT);
            else if (i == randomVer[3] && j == randomHor[3] && i != BoardButtons.length)
                BoardButtons[i][j].setText(Game.DIRECTION_DOWN);
            else if (i == randomVer[4] && j == randomHor[4] && j != BoardButtons[i].length)
                BoardButtons[i][j].setText(Game.DIRECTION_RIGHT);
            else if (i == randomVer[5] && j == randomHor[5] && i != BoardButtons.length)
                BoardButtons[i][j].setText(Game.DIRECTION_DOWN);
            else
                BoardButtons[i][j].setText(Game.EMPTY_SPACE);
            BoardButtons[i][j].setEnabled(true);
            BoardButtons[i][j].setOnClickListener(new ButtonClick(i, j));
        }
    }
    gameOver = false;
}

public class ButtonClick implements View.OnClickListener{

int locationVer;
    int locationHor;
    public ButtonClick(int locationVer, int locationHor)
    {
        this.locationVer = locationVer;
        this.locationHor = locationHor;
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            //kicks
            case R.id.button1:
                playAudio[0].start();
                break;
            case R.id.button2:
                playAudio[1].start();
                break;
            case R.id.button3:
                playAudio[2].start();
                break;
            case R.id.button4:
                playAudio[3].start();
                break;
            case R.id.button5:
                playAudio[4].start();
                break;
            case R.id.button6:
                playAudio[5].start();
                break;
            case R.id.button7:
                playAudio[6].start();
                break;
            //snares
            case R.id.button8:
                playAudio[7].start();
                break;
            case R.id.button9:
                playAudio[8].start();
                break;
            case R.id.button10:
                playAudio[9].start();
                break;
            case R.id.button11:
                playAudio[10].start();
                break;
            case R.id.button12:
                playAudio[11].start();
                break;
            case R.id.button13:
                playAudio[12].start();
                break;
        }
    }
}

This is my layout activity:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:background="#800008">

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:layout_alignParentTop="false"
    android:id="@+id/tableLayout"
    android:layout_alignWithParentIfMissing="false"
    android:gravity="center">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal">

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button1"
            android:layout_column="1"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button2"
            android:layout_column="2"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button3"
            android:layout_column="3"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button4"
            android:layout_column="4"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button5"
            android:layout_column="5"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button6"
            android:layout_column="6"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button7"
            android:layout_column="7"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal">

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button8"
            android:layout_column="1"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button9"
            android:layout_column="2"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button10"
            android:layout_column="3"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button11"
            android:layout_column="4"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button12"
            android:layout_column="5"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button13"
            android:layout_column="6"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button14"
            android:layout_column="7"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal">

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button15"
            android:layout_column="1"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button16"
            android:layout_column="2"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button17"
            android:layout_column="3"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button18"
            android:layout_column="4"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button19"
            android:layout_column="5"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button20"
            android:layout_column="6"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button21"
            android:layout_column="7"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal">

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button22"
            android:layout_column="1"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button23"
            android:layout_column="2"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button24"
            android:layout_column="3"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button25"
            android:layout_column="4"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button26"
            android:layout_column="5"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button27"
            android:layout_column="6"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button28"
            android:layout_column="7"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal">

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button29"
            android:layout_column="1"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button30"
            android:layout_column="2"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button31"
            android:layout_column="3"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button32"
            android:layout_column="4"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button33"
            android:layout_column="5"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button34"
            android:layout_column="6"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button35"
            android:layout_column="7"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal">

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button36"
            android:layout_column="1"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button37"
            android:layout_column="2"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button38"
            android:layout_column="3"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button39"
            android:layout_column="4"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button40"
            android:layout_column="5"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button41"
            android:layout_column="6"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button42"
            android:layout_column="7"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal">

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button43"
            android:layout_column="1"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button44"
            android:layout_column="2"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button45"
            android:layout_column="3"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button46"
            android:layout_column="4"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button47"
            android:layout_column="5"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button48"
            android:layout_column="6"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />

        <Button
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:id="@+id/button49"
            android:layout_column="7"
            android:background="@drawable/custom_button_field"
            android:textSize="10dp" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </TableRow>
</TableLayout>

<TextView
    android:layout_width="200dp"
    android:layout_height="40dp"
    android:id="@+id/textView2"
    android:background="#aa0008"
    android:textColor="#ffffff"
    android:textSize="30dp"
    android:gravity="center"
    android:layout_below="@+id/tableLayout"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp" />

<Button
    android:layout_width="120dp"
    android:layout_height="120dp"
    android:text=""
    android:id="@+id/buttonRestart"
    android:background="@drawable/custom_button_restart"
    android:layout_below="@+id/textView2"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_marginRight="30dp"
    android:layout_marginTop="10dp" />

<Button
    android:layout_width="120dp"
    android:layout_height="120dp"
    android:text=""
    android:id="@+id/buttonExit"
    android:background="@drawable/custom_back_button"
    android:layout_below="@+id/textView2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="30dp" />
</RelativeLayout>

It have already set audio to 12/49 buttons. The app crashes when I click buttons 5, 6, 7, 10, 11, 12.

1

There are 1 answers

5
Lejacque  Gillespie On

This is a copy of a code I have that runs, hope it helps.

public class MainActivity extends Activity {

MediaPlayer button_sound;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button b1 = (Button) findViewById(R.id.button1);
    Button b2 = (Button) findViewById(R.id.button2);
    Button b3 = (Button) findViewById(R.id.button3);
    Button b4 = (Button) findViewById(R.id.button4);

    b1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            button_sound = MediaPlayer.create(MainActivity.this,
                    R.raw.shooting);
            button_sound.start();
        }
    });
    b2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            button_sound = MediaPlayer.create(MainActivity.this,
                    R.raw.rounds);
            button_sound.start();
        }
    });
    b3.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            button_sound = MediaPlayer.create(MainActivity.this,
                    R.raw.shooting);
            button_sound.start();
        }
    });
    b4.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            button_sound = MediaPlayer.create(MainActivity.this,
                    R.raw.rounds);
            button_sound.start();
        }
    });

}