我有一个按钮阵列,我想点击每个按钮播放不同的声音。当我点击某些按钮时一切正常,但是当我点击其他按钮时,应用程序崩溃了,logcat说:
java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.media.MediaPlayer.start()'
这是我的代码示例:
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;
}
}
}
这是我的布局活动:
<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>
它已将音频设置为12/49按钮。单击按钮5
,6
,7
,10
,11
,12
时,应用会崩溃。
答案 0 :(得分:0)
这是我运行的代码的副本,希望它有所帮助。
公共类MainActivity扩展了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();
}
});
}