我正在尝试开发一个在单击按钮时播放不同鼓声的应用程序。使用我当前的代码,当我尝试单击按钮时,我得到了一个空指针异常。
这是我的代码:
package com.example.pocketbeatbox;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
MediaPlayer kick;
MediaPlayer snare;
MediaPlayer clap;
MediaPlayer openHat;
MediaPlayer closedHat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
kick = MediaPlayer.create(this, R.raw.kick);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void playKick(View view) {
kick.start();
kick.release();
};
}