我正在尝试创建一个媒体播放器,它将更新一个搜索栏,告诉正在播放的歌曲的当前位置。我试过这段代码,但似乎有些不对劲。有人可以帮我解决这个错误吗?
这是我的代码
public class AlbumDetails extends Activity implements OnClickListener {
private ImageView play, forward, backward;
private MediaPlayer mediaPlayer;
private boolean playing = false;
private ProgressDialog dialog;
private String mp3link;
private SeekBar seekbar;
private Handler handler;
private int mediaPos;
private int mediaMax;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playeer);
initWidgets();
}
private void initWidgets() {
play = (ImageView) findViewById(R.id.playeer_play);
play.setOnClickListener(this);
seekbar = (SeekBar) findViewById(R.id.playeer_seek);
forward = (ImageView) findViewById(R.id.playeer_forward);
backward = (ImageView) findViewById(R.id.playeer_back);
mediaPlayer = new MediaPlayer();
mp3link = getIntent().getStringExtra("mp3link");
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.playeer_play:
playFunction();
break;
}
}
private void playFunction() {
if (!playing) {
try {
dialog = ProgressDialog
.show(AlbumDetails.this,
"",
getString(com.darkovski.quran.R.string.buffering),
true);
dialog.setCancelable(true);
dialog.show();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(mp3link);
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
play.setBackgroundResource(R.drawable.pause);
playing = true;
//this is new
mediaPos = mp.getCurrentPosition();
mediaMax = mp.getDuration();
seekbar.setMax(mediaMax);
seekbar.setProgress(mediaPos);
//this line is the error
handler.removeCallbacks(moveSeekBarThread);
handler.postDelayed(moveSeekBarThread, 100);
mp.start();
dialog.dismiss();
}
});
mediaPlayer.prepareAsync();
} catch (Exception e) {
e.printStackTrace();
dialog.dismiss();
}
} else {
play.setBackgroundResource(R.drawable.play_play);
mediaPlayer.stop();
mediaPlayer.release();
playing = false;
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
if (mediaPlayer.isPlaying())
mediaPlayer.stop();
mediaPlayer.release();
}
private Runnable moveSeekBarThread = new Runnable() {
public void run() {
if (mediaPlayer.isPlaying()) {
int mediaPos_new = mediaPlayer.getCurrentPosition();
int mediaMax_new = mediaPlayer.getDuration();
seekbar.setMax(mediaMax_new);
seekbar.setProgress(mediaPos_new);
handler.postDelayed(this, 100); // Looping the thread after 0.1
// second
// seconds
}
}
};
}
我从logcat获得这个:
12-07 15:38:27.148: E/AndroidRuntime(2835): FATAL EXCEPTION: main
12-07 15:38:27.148: E/AndroidRuntime(2835): java.lang.NullPointerException
12-07 15:38:27.148: E/AndroidRuntime(2835): at com.darkovski.quran.AlbumDetails$2.onPrepared(AlbumDetails.java:78)
12-07 15:38:27.148: E/AndroidRuntime(2835): at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:1523)
12-07 15:38:27.148: E/AndroidRuntime(2835): at android.os.Handler.dispatchMessage(Handler.java:99)
12-07 15:38:27.148: E/AndroidRuntime(2835): at android.os.Looper.loop(Looper.java:137)
12-07 15:38:27.148: E/AndroidRuntime(2835): at android.app.ActivityThread.main(ActivityThread.java:4441)
12-07 15:38:27.148: E/AndroidRuntime(2835): at java.lang.reflect.Method.invokeNative(Native Method)
12-07 15:38:27.148: E/AndroidRuntime(2835): at java.lang.reflect.Method.invoke(Method.java:511)
12-07 15:38:27.148: E/AndroidRuntime(2835): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-07 15:38:27.148: E/AndroidRuntime(2835): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-07 15:38:27.148: E/AndroidRuntime(2835): at dalvik.system.NativeStart.main(Native Method)
有人可以告诉我问题在哪里,我该如何解决?
答案 0 :(得分:2)
您的handler
是null
,因为您尚未将该数据成员初始化为任何内容。