我在原始文件夾中放置了兩首mp3歌曲。我用兩個按鈕來播放它們。我叫它如下:在mediaplayer中停止和播放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)
任何人都可以請解決這個問題嗎? 謝謝
代碼中哪行是99行? – LuigiEdlCarno