所以我正在使用这段代码来访问我想要播放的歌曲的数据,但是每当我使用媒体播放器时我都会收到空指针异常。我用 toast 来检查文件名,即文件的路径是否正确,但它即将到来出来就好了。
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
// super.onListItemClick(l, v, position, id);
// l.setClickable(false);
music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
musiccursor.moveToPosition(position);
String filename = musiccursor.getString(music_column_index);
try {
mMediaPlayer.setDataSource(filename);
mMediaPlayer.prepare();
mMediaPlayer.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(getApplicationContext(),filename, Toast.LENGTH_LONG).show();
}
我在这条线上遇到错误
mMediaPlayer.setDataSource(filename);
日志猫:-
06-17 17:31:36.716: E/AndroidRuntime(5059): FATAL EXCEPTION: main
06-17 17:31:36.716: E/AndroidRuntime(5059): java.lang.NullPointerException
06-17 17:31:36.716: E/AndroidRuntime(5059): at prac.tise_music.MainActivity.onListItemClick(MainActivity.java:136)
06-17 17:31:36.716: E/AndroidRuntime(5059): at android.app.ListActivity$2.onItemClick(ListActivity.java:342)
06-17 17:31:36.716: E/AndroidRuntime(5059): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
06-17 17:31:36.716: E/AndroidRuntime(5059): at android.widget.ListView.performItemClick(ListView.java:3569)
06-17 17:31:36.716: E/AndroidRuntime(5059): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1831)
06-17 17:31:36.716: E/AndroidRuntime(5059): at android.os.Handler.handleCallback(Handler.java:587)
06-17 17:31:36.716: E/AndroidRuntime(5059): at android.os.Handler.dispatchMessage(Handler.java:92)
06-17 17:31:36.716: E/AndroidRuntime(5059): at android.os.Looper.loop(Looper.java:150)
06-17 17:31:36.716: E/AndroidRuntime(5059): at android.app.ActivityThread.main(ActivityThread.java:4389)
06-17 17:31:36.716: E/AndroidRuntime(5059): at java.lang.reflect.Method.invokeNative(Native Method)
06-17 17:31:36.716: E/AndroidRuntime(5059): at java.lang.reflect.Method.invoke(Method.java:507)
06-17 17:31:36.716: E/AndroidRuntime(5059): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
06-17 17:31:36.716: E/AndroidRuntime(5059): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
06-17 17:31:36.716: E/AndroidRuntime(5059): at dalvik.system.NativeStart.main(Native Method)