public void initMedia(String url) throws IOException {
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(url);
mediaPlayer.prepare();
} catch (Exception e){
Toast.makeText(getApplication(),"出了点问题!",Toast.LENGTH_SHORT).show();
}
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
SongsDetail songsdetail = (SongsDetail) msg.obj;
try {
Log.d(TAG, "handleMessage: 创建音乐对象");
initMedia(songsdetail.getUrl());
seekBar.setMax(mediaPlayer.getDuration());
textView_total.setText(getTime(mediaPlayer.getDuration()));
textView_songsTitle.setText(songsdetail.getSongName());
} catch (IOException e) {
e.printStackTrace();
}
}
};
错误日志是这样的
E/AndroidRuntime: FATAL EXCEPTION: main
Process: hnkjzyxy.class3202.ending, PID: 13471
java.lang.NullPointerException: uriString
at android.net.Uri$StringUri.<init>(Uri.java:476)
at android.net.Uri$StringUri.<init>(Unknown Source:0)
at android.net.Uri.parse(Uri.java:438)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1147)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1141)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1108)
at hnkjzyxy.class3202.ending.SongsDetailActivity.initMedia(SongsDetailActivity.java:152)
at hnkjzyxy.class3202.ending.SongsDetailActivity$1.handleMessage(SongsDetailActivity.java:87)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
希望有人可以给我解释一下,错误原因以及解决方案,十分感谢