我在 raw 文件夹中放了两首 mp3 歌曲。我使用了两个按钮来播放它们。我这样称呼它:
public class Songs extends Activity implements OnClickListener {
private Button mBtn1, mBtn2, mBtn3, mBtn4, mBtn5, mBtn6, mBtn7, mBtn8, mBtn9, mBtn10;
MediaPlayer mp;
boolean playing = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.songs);
mBtn1 = (Button) findViewById(R.id.button1);
mBtn2 = (Button) findViewById(R.id.button2);
mBtn1.setOnClickListener(this);
mBtn2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
else if (v.getId() == mBtn1.getId()) {
if (playing) {
mp.stop();
mp.pause();
playing = false;
} else {
MediaPlayer mp = MediaPlayer.create(Songs.this, R.raw.chahun);
mp.start();
playing = true;
// mp.release();
}
} else if (v.getId() == mBtn2.getId()) {
if (playing) {
mp.stop();
mp.pause();
playing = false;
} else {
MediaPlayer mp = MediaPlayer.create(Songs.this, R.raw.sunn);
mp.start();
playing = true;
// mp.release();
}
}
}
}
但我认为在停止歌曲时会出错。我收到如下错误:
05-21 11:49:23.006: E/AndroidRuntime(15436): FATAL EXCEPTION: main
05-21 11:49:23.006: E/AndroidRuntime(15436): java.lang.NullPointerException
05-21 11:49:23.006: E/AndroidRuntime(15436): at com.qwinix.lucia.Songs.onClick(Songs.java:99)
05-21 11:49:23.006: E/AndroidRuntime(15436): at android.view.View.performClick(View.java:3517)
05-21 11:49:23.006: E/AndroidRuntime(15436): at android.view.View$PerformClick.run(View.java:14155)
05-21 11:49:23.006: E/AndroidRuntime(15436): at android.os.Handler.handleCallback(Handler.java:605)
05-21 11:49:23.006: E/AndroidRuntime(15436): at android.os.Handler.dispatchMessage(Handler.java:92)
05-21 11:49:23.006: E/AndroidRuntime(15436): at android.os.Looper.loop(Looper.java:154)
05-21 11:49:23.006: E/AndroidRuntime(15436): at android.app.ActivityThread.main(ActivityThread.java:4624)
05-21 11:49:23.006: E/AndroidRuntime(15436): at java.lang.reflect.Method.invokeNative(Native Method)
05-21 11:49:23.006: E/AndroidRuntime(15436): at java.lang.reflect.Method.invoke(Method.java:511)
05-21 11:49:23.006: E/AndroidRuntime(15436): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
05-21 11:49:23.006: E/AndroidRuntime(15436): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
05-21 11:49:23.006: E/AndroidRuntime(15436): at dalvik.system.NativeStart.main(Native Method)
任何人都可以解决这个问题吗?谢谢