//停止媒体播放器的方法
private void stopPlaying() {
if (mp.isPlaying()) {
mp.stop();
mp.release();
mp = null;
}
}
//搜索栏和时间的playCycle方法
public void playCycle(){
mSeekBar.setProgress(mp.getCurrentPosition());
if (mp.isPlaying()){
runnable = new Runnable() {
@Override
public void run() {
playCycle();
long totalDuration = mp.getDuration();
long currentDuration = mp.getCurrentPosition();
crnt.setText(milliSecondsToTimer(currentDuration));
ttl.setText(milliSecondsToTimer(totalDuration));
}
};
handler.postDelayed(runnable, 100);
}
}
Logcat-行203,209是指mSeekBar.setProgress和playCycle()。
08-13 17:57:16.215 21164-21164 / com.example.tsuryohananov.voicerecorder E / AndroidRuntime:FATAL EXCEPTION:main 处理:com.example.tsuryohananov.voicerecorder,PID:21164 java.lang.NullPointerException:尝试在空对象引用上调用虚方法'int android.media.MediaPlayer.getCurrentPosition()' 在com.example.tsuryohananov.voicerecorder.RecordsActivity.playCycle(RecordsActivity.java:203) 在com.example.tsuryohananov.voicerecorder.RecordsActivity $ 6.run(RecordsActivity.java:209) 在android.os.Handler.handleCallback(Handler.java:769) 在android.os.Handler.dispatchMessage(Handler.java:98) 在android.os.Looper.loop(Looper.java:164) 在android.app.ActivityThread.main(ActivityThread.java:6540) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)